Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将事件侦听器附加到android UI组件网?_Java_Android - Fatal编程技术网

Java 将事件侦听器附加到android UI组件网?

Java 将事件侦听器附加到android UI组件网?,java,android,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.*; p

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);
  }


}
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" />


要添加OnTouchListener类型的新匿名类,如下所示:

tv.setOnTouchListener(new OnTouchListener() {
    @Override
    boolean onTouch(View v, MotionEvent event){
        //handle touch here
        return true; //if handled
    )
});

tv.setOnTouchListener((OnTouchListener)this)当前无效,因为您的
不是
OnTouchListener
并且它也没有实现
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)
}

添加此行setContentView(R.layout.linear);在super.onCreate(savedInstanceState)之后;此外,正如@Samir所说,您需要更改设置内容视图的顺序。您无法找到您的文本视图
tv
,直到它存在,这不是在您设置视图之前。
tv.setOnTouchListener(new OnTouchListener() {
    @Override
    boolean onTouch(View v, MotionEvent event){
        // TODO Auto-generated method stub
        return true; 
    )
});