Android:onTouch()永远不会被调用?

Android:onTouch()永远不会被调用?,第1张

概述我正在玩UI事件处理,我找到了一些我无法从 Android Dev找到解释的东西:我有一个ImageView和一个TextView,每当我触摸ImageView时,TextView都会显示一条消息.但以下代码不起作用: public class ShowSomething extends Activity { private LinearLayout ll; public void onCr 我正在玩UI事件处理,我找到了一些我无法从 Android Dev找到解释的东西:我有一个ImageVIEw和一个TextVIEw,每当我触摸ImageVIEw时,TextVIEw都会显示一条消息.但以下代码不起作用:
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()永远不会被调用?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1137023.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存