ImageView中的OnTouchListener无法正常工作

时间:2014-02-11 06:12:28

标签: 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);
    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

来解决

0 个答案:

没有答案