Android的 - 两个的onClick监听器和一个按钮 [英] Android - Two onClick listeners and one button

查看:157
本文介绍了Android的 - 两个的onClick监听器和一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的TextView是点击。它定义为了改变根据点击它的外观了自己的onClick处理程序。但是被点击,如果我再定义为了做基础上的按钮东西在我的活动第二的onClick处理程序,只的onclick功能之一被调用。的onClick是一个void函数 - 有什么办法说我没有处理此点击,请把它传给其他的onClick处理程序?

I have a custom TextView which is clickable. It defines its own onClick handler in order to change its appearance based on clicks. However if I then define a second onClick handler in my activity in order to do something based on the button being clicked, only one of the onClick functions is called. onClick is a void function - is there any way to say I didn't process this click, please pass it on to other onClick handlers?

要在这里更清楚的是code:

To be more clear here is the code:

里面MyCheckButton延伸的TextView我有:

Inside MyCheckButton which extends TextView I have:

    setOnClickListener( mClickListener );

    private OnClickListener mClickListener = new OnClickListener() {
        public void onClick(View v) {
            toggle();
        }
    };

不过,我有MyCheckButton到我的活动,当然我需要做一些事情的时候它的点击,所以我将另一个OnClickListener到它:

However I include MyCheckButton into my Activity, and of course I need to do something when its clicked so I attach another OnClickListener to it:

MyCheckButton button= (MyCheckButtonButton) findViewById(R.id.cb);
button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // do something in the app
    }

});

通过调用setOnClickListener两次看来,我取代了原来的侦听器,切换(),它的外观变化不会被调用。我如何,如果它已经在使用的onClick处理程序,以改变其外观做我的东西的活动单击此按钮时?我以为我会只是看到这两个OnClickListeners获取调用。

By calling setOnClickListener twice it appears that I am replacing the original listener so toggle() which changes the appearance is never called. How can I do something in my activity when this button is clicked if it is already using the onClick handler to change its appearance? I thought I would simply see both OnClickListeners getting called.

推荐答案

这是一个有点脏,但如果你需要多个监听我会做到这一点的方法就是注册一个知道对方。然后第一个(这实际上是注册的)需要知道什么时候委托给基于事件的条件以外,其它监听器。其实,在现实中,有没有真正的需要有两个 OnClickListener 类。第二类可以实现任何你想要的界面。此外,没有必要创造你所需要的特殊接口。

This is a bit dirty, but the way I would do this if you need multiple listeners is to register one that knows about the other. The first one (the one that's actually registered) will then need to know when to delegate to the other listener based on the conditions of the event. Actually, in reality, there's no real need to have two OnClickListener classes. The second class can implement whatever interface you want. Additionally, there's no need to create a special interface for what you need.

public class MyClickListener implements OnClickListener{
  private SomeCustomClass mSecondListener = new SomeCustomClass();
  public void onClick(View v){
    if (needToForward){
      mSecondListener.handleClick(v);
    }else{
      //handle the click
    }
  }
}

然后,在你的code为您的活动,你可以这样做

Then, in your code for your activity, you would do this

MyClickListener lstn = new MyClickListener();
mCheckBox.setOnClickListener(lstn);

是否有一个原因,这不会为你工作?

Is there a reason this wouldn't work for you?

另外,如果你想要的,第二类也可以实施 OnClickListener 接口。

Alternatively, if you wanted, the second class could also implement the OnClickListener interface.

此外,如果你需要真正的气泡,你可以定义自己的接口,支持添加多个点击听众,恰好实现了 OnClickListener 接口的中级班。从那里,在那类的的onClick()方法,你将通过注册的侦听器调用适当的方法进行迭代。

Additionally, if you need true bubbling, you could define your own interface that supports adding multiple click listeners to an intermediate class that happens to implement the OnClickListener interface. From there, in that class's onClick() method, you would iterate through the registered listeners calling the appropriate method.

这篇关于Android的 - 两个的onClick监听器和一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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