简体   繁体   English

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

[英]Attaching event listeners to an android UI componenet?

android 3.0 API Level 11 android 3.0 API等级11

How do I attach an onTouch Event to the button? 如何将onTouch事件附加到按钮?

TestDebugingActivity.java 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 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); is invalid currently because your this is not an OnTouchListener and it does not implement the OnTouchListener interface either. 目前无效,因为您的this不是OnTouchListener ,也没有实现OnTouchListener接口。

There are multiple ways this can be solved. 有多种方法可以解决这个问题。 An easy way (not necessarily best) is to have your activity implement OnTouchListener. 一种简单的方法(不一定是最好的)是让您的活动实现OnTouchListener。 To do that, you'll need to declare it and you'll need to add a single method: 要做到这一点,你需要声明它,你需要添加一个方法:

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

You want to add a new anonymous class of the type OnTouchListener, like this: 您想要添加OnTouchListener类型的新匿名类,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM