Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.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
Android ImageView中的OnTouchListener不工作_Android_Android Imageview_Ontouchlistener - Fatal编程技术网

Android ImageView中的OnTouchListener不工作

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

在下面的代码中,永远不会调用onTouch()方法。下面是我扩展ImageView类的类

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解决了此问题。谢谢你,戈帕尔。