
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); }} 和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"><TextVIEwandroID:ID="@+ID/textvIEw"androID:layout_wIDth="fill_parent"androID:layout_height="wrap_content"androID:text="Holder"/></linearLayout>
但是当我像这样在MyImageVIEw上附加一个OntouchListener时,它确实有效:
文件ShowSomething.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); } } 并提交MyImageVIEw.java文件
public class MyImageVIEw extends ImageVIEw { private TextVIEw textvIEw; public MyImageVIEw(Context context,TextVIEw textvIEw) { super(context); this.textvIEw = textvIEw; }} 但据我所知,2实现是相同的(实现事件监听器) – 我是否误解了什么?
解决方法 对于第一种情况,正确的方法是:MyImageVIEw image = new MyImageVIEw(this,textvIEw);image.setimageResource(R.drawable.icon);image.setontouchListener(image);
或者在MyImageVIEw类中调用setontouchListener(this).
总结以上是内存溢出为你收集整理的Android:onTouch()永远不会被调用?全部内容,希望文章能够帮你解决Android:onTouch()永远不会被调用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)