2010-12-08 24 views
7

UI Event Handling ile oynuyor ve Android Dev'dan açıklama bulamadığım bir şey buldum: ImageView'a sahip olduğum bir ImageView ve TextView var, TextView bir iletiyi gösterir. Ancak aşağıdaki kodları çalışmaz:Android: onTouch() asla çağrılmıyor mu?

public class ShowSomething extends Activity { 
private LinearLayout ll; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout); 
    final TextView textview = (TextView)findViewById(R.id.textview); 
    MyImageView image = new MyImageView(this, textview); 
    image.setImageResource(R.drawable.icon); 
    ll.addView(image, 48, 48); 

} 
} 

Ve MyImageView.java

public class MyImageView extends ImageView implements OnTouchListener{ 


private TextView textview; 

public MyImageView(Context context,TextView textview) { 
    super(context); 
    this.textview = textview; 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    textview.setText("Event captured !"); 
    return true; 
} 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
> 
<TextView 
android:id="@+id/textview" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Holder" 
/> 
</LinearLayout> 

Ama hiç böyle MyImageView bir OnTouchListener taktığınızda, onu çalışır: Dosya GösterisiSomething.java

public class ShowSomething extends Activity { 

private LinearLayout ll; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout); 
    final TextView textview = (TextView)findViewById(R.id.textview); 


    MyImageView image = new MyImageView(this,textview); 
    image.setImageResource(R.drawable.icon); 
    image.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     textview.setText("Event captured!");  
     return false; 
    } 
    }); 
    ll.addView(image, 48, 48); 

} 

} 

Ve dosya MyImageView.java

public class MyImageView extends ImageView { 


private TextView textview; 

public MyImageView(Context context,TextView textview) { 
    super(context); 
    this.textview = textview; 
} 


} 

Ama bildiğim kadarıyla, 2 uygulama (Olay İşleyici uygulamak) aynıdır - Bir şey yanlış mı?

cevap

9

ilk durumda, doğru yolu olacaktır:

MyImageView image = new MyImageView(this, textview); 
image.setImageResource(R.drawable.icon); 
image.setOnTouchListener(image); 

veya MyImageView sınıf içinde setOnTouchListener(this) diyoruz.

+0

Teşekkür ederim Reuben & İsa :) – hungson175

11

İlk uygulamanız dinleyiciyi ayarlamayı unuttu ... yapıcıda setOnTouchListener(this) aramasını yapmalı ve çalışmalıdır.

İlgili konular