java - 将事件监听器附加到 Android UI 组件?

标签 java android

Android 3.0 API 级别 11

如何将 onTouch 事件附加到按钮?

TestDebugingActivity.java

package com.example.debuggingTest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.*;

public class TestDebugingActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    TextView tv = (TextView)findViewById(R.id.textView1);
    tv.setOnTouchListener((OnTouchListener) this);


    setContentView(R.layout.linear);
  }


}

线性.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >



<TextView
    android:id="@+id/textView1"
    android:layout_width="170dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Firstname:"
    android:textAppearance="?android:attr/textAppearanceMedium" />


<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>


<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Lastname:"
    android:textAppearance="?android:attr/textAppearanceMedium" />


<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:inputType="textPersonName" />


<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="E-mail:"
    android:textAppearance="?android:attr/textAppearanceMedium" />


<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:inputType="textEmailAddress" />



<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:text="Gender:"
    android:textAppearance="?android:attr/textAppearanceMedium" />





<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="224dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="horizontal" >


    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:checked="true"
        android:text="Male:" />


    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Female:" />

</RadioGroup>







<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:text="Register" />

最佳答案

tv.setOnTouchListener((OnTouchListener) this); 当前无效,因为您的 this 不是 OnTouchListener 并且它没有实现OnTouchListener 接口(interface)。

有多种方法可以解决这个问题。一个简单的方法(不一定是最好的)是让您的 Activity 实现 OnTouchListener。为此,您需要声明它并添加一个方法:

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return false; // (false: let the event bubble up; true: you handled it)
}

关于java - 将事件监听器附加到 Android UI 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9725067/

相关文章:

java - 将带有 css 的 Jsp 转换为 pdf

java - Android 中的 XML 解析器

java - 由 : java. lang.IllegalArgumentException : Binary XML file line #9: Must specify unique android:id, android :tag, 引起,或者有一个 id 为

android - 如何自定义 Google Play 服务以减小 jar 大小?

扩展类的 Java 泛型未检查警告

android - 为什么我的带有 CheckBox id 的 ListView 变得困惑了?

java - 无法从 ListView 获取值微调器

android - 具有 ListView 和多对多关系的内容提供者

java - 如何解压包含子目录的 zip 存档?

java - 搜索 .jar 文件 eclipse