Android ImageView中的OnTouchListener不工作
在下面的代码中,永远不会调用onTouch()方法。下面是我扩展ImageView类的类Android ImageView中的OnTouchListener不工作,android,android-imageview,ontouchlistener,Android,Android Imageview,Ontouchlistener,在下面的代码中,永远不会调用onTouch()方法。下面是我扩展ImageView类的类 public class MyImageView extends ImageView implements OnTouchListener{ private TextView textview; public MyImageView(Context context, TextView textview) { super(context); setOnTouchListener(this);
public class MyImageView extends ImageView implements OnTouchListener{
private TextView textview;
public MyImageView(Context context, TextView textview) {
super(context);
setOnTouchListener(this);
textview.setText("Reached here !");
this.textview = textview;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
textview.setText("Event captured !");
return true;
}
}
主要活动,
public class MyMainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textview = (TextView)findViewById(R.id.textView1);
MyImageView imageview = new MyImageView(this, textview);
imageview.setImageResource(R.drawable.myimage);
LinearLayout ll1 = (LinearLayout)findViewById(R.id.linearlayout01);
ll1.addView(imageview);
}
}
和main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<LinearLayout
android:id="@+id/linearlayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
谢谢
通过将ImageView添加到LinearLayout为什么不单击Listener..来解决此问题。您是否将此
MyImageView
添加到Activity
layout?将MyImageView添加到Activity layout解决了此问题。谢谢你,戈帕尔。