OnTouch工作,但OnClickListener不? [英] OnTouch works, but OnClickListener doesn't?

查看:122
本文介绍了OnTouch工作,但OnClickListener不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小窗口/控制,我可以重复使用,我通过扩展RelativeLayout的类创建。然后,在我活动之一,我创建了一堆这些小部件的循环。然而,我遇到了一个问题,当我想有一个点击每个插件响应。

I created a widget/control that I can reuse which I created by extending the RelativeLayout class. Then, in one of my Activities, I create a bunch of these widgets in a loop. However, I ran into an issue when I wanted to have each widget respond to a click.

我发现OnTouch事件侦听器的工作原理:

I found that the OnTouch event listener works:

 this.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View arg0, MotionEvent arg1) {
           //Triggers debug message
        }       
    });

但将OnClick不??

but the OnClick doesn't??

  this.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
           //Doesn't trigger debug message  
        }

    });

这是为什么?

Why is this?

推荐答案

下面有几件事情要检查,以确保您的看法是可以点击的:

Here are a few things to check to make sure your views are clickable:

View.setClickable()
View.setEnabled()
View.setFocusable()
View.setFocusableInTouchMode()

根据你希望得到确切的行为,您需要设置其中的一个对一种或多种。既然你得到onTouch事件,我的猜测是,你需要setClickable。但我不得不看到视图创建code是肯定的。

Depending on the exact behavior you hope to get, you'll need to set one or more of those to on. Since you are getting onTouch events, my guess is that you need to setClickable. But i'd have to see the view creation code to be sure.

这篇关于OnTouch工作,但OnClickListener不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆