Skip to content Skip to sidebar Skip to footer

Keeping Multiple Onclicklisteners In One Class

I am kinda new to Android and it would improve my application a lof if I coul keep several OnClickListenres in one class. What I am thiking of is something like this : Public class

Solution 1:

There is a sleek way to consolidate all your anonymous classes into one and switch on the view. This works best if you know ahead of time which buttons will be using the clicklistener :

publicclassAndroidTestClickListenerActivityextendsActivity {

    /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Buttonbutton1= (Button) findViewById(R.id.button1);
        button1.setOnClickListener(newMyClickListener());

        Buttonbutton2= (Button) findViewById(R.id.button2);
        button2.setOnClickListener(newMyClickListener());

        Buttonbutton3= (Button) findViewById(R.id.button3);
        button3.setOnClickListener(newMyClickListener());
    }


}

classMyClickListenerimplementsView.OnClickListener {
    @OverridepublicvoidonClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.button1:
                // do soemthign for button1break;
            case R.id.button2:
                // do something for button2break;
            case R.id.button3:
                // do something for button3break;
            default:
                // do something for any other button

        }

    }
}

Solution 2:

You can write

button1.setOnClickListener(new OnClickListeners().open);  

instead, but this seems an odd architecture for me. I'd suggest you to keep 1 listener in 1 file, having all of them in 1 package, and use like

button1.setOnClickListener(newOpenListener());  

Solution 3:

The problem of you approach is that usually listeners have to manipulate some data that is part of the class where UI elements are.

If you take listeners out and put them in a separate class, you will also have to provide a lot of references to objects where data to be manipulated is. This will create a lot of interdependent classes, which will not be nice.

IMHO the cleanest way is to use anonymous inner classes.

Solution 4:

You can, but you have to declare the OnClickListener as static if you would like to use it in this manner.

publicstatic Button.OnClickListeneropenListener=newButton.OnClickListener() {
     publicvoidonClick(View view) {

     }
};

Then you can use:

button1.setOnClickListener(OnClickListeners.openListener);

As noted by other user - this approach is most like bad. You should handle view listeners on the same view and then maybe call another method like openActivity(). I would not do this - you are also openning an activity from another activity, this will probably don't work at all or will mess up the activity history stack

Solution 5:

As none of the solutions actually did what I wanted to achieve - I needed a second (or multiple) onClickListener that did not override the onClickListeners that were already assigned to the control.

Here is the java class that I wrote for that purpose:

https://gist.github.com/kosiara/c090dcd684ec6fb2ac42#file-doubleclicklistenerimagebutton-java

publicclassDoubleClickListenerImageButtonextendsImageButton {

    View.OnClickListener mSecondOnClickListener;

    publicDoubleClickListenerImageButton(Context context) {
       super(context);
    }

    [...]

    publicvoidsetSecondOnClickListener(View.OnClickListener l) {
        mSecondOnClickListener = l;
    }

    @OverridepublicbooleanperformClick() {
       if (mSecondOnClickListener != null)
          mSecondOnClickListener.onClick(this);
       returnsuper.performClick();
    }

    @OverridepublicbooleanperformContextClick() {
       if (mSecondOnClickListener != null)
          mSecondOnClickListener.onClick(this);
       returnsuper.performContextClick();
    }
}

Post a Comment for "Keeping Multiple Onclicklisteners In One Class"