문제

Is it possible to attach multiple onClick listeners to buttons in android? Example:

btn1.setOnClickListener(listener1);
btn1.setOnCliclListener(listener2);

And when the button is clicked onClick event of both listener1 and listener2 are triggered?

In normal GUI applications I know that we can attach more than one event handler for an event. Is it possible for android too or is there any restrictions?

Regards, Lalith

도움이 되었습니까?

해결책

Android only supports one registered listener in general. However, you can easily create a listener that simply forwards the events to other listeners using the composite pattern.

다른 팁

public void onClick(View v) {
    if(v.getId() == R.id.button1) {
        // do this
    }else if(v.getId() == R.id.button2) {
        // do that
    }
}

Nope, for example just do this :

Set Listener:

btn.setOnClickListener(this);

Implement Method:

public void Onclick(View arg0){

   // check your id and do what you want
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top