2012-03-15 4 views
0

android 3.0 APIレベル11アンドロイドUIコンポーネントにイベントリスナーをアタッチしますか?

どのようにボタンに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); 
    } 


} 

あなたはこのように、型OnTouchListenerの新しい匿名クラスを追加したい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" /> 

+1

このラインsetContentView(R.layout.linear)を追加します。 super.onCreate(savedInstanceState)の後に。 –

答えて

2

tv.setOnTouchListener((OnTouchListener) this);thisOnTouchListenerではなく、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) 
} 
+0

@Samirによると、コンテンツビューの設定順序を変更する必要があります。あなたのTextView 'tv'が存在するまで見つけることができません。これはビューを設定する前ではありません。 – mah

0

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