How to add a common listener for all buttons (no intrusive)?

I try to play a sound when you press any application button.

How is a secondary intrusive way to add a listener to all buttons in an application?

Is it possible to add overwrite (and expand) the common Android receiver?

Thank.

+5
source share
6 answers

You can set one listener for all buttons and can also identify the tag.

View.OnClickListener myClickLIstener= new View.OnClickListener() {
    public void onClick(View v) {
        String tag = (String) v.getTag();
        Log.e("","tag : "+tag)
       // your stuff
    }
};

tuning listeners ...

btn1.setOnClickListener(myClickLIstener);
btn1.setTag("btn1");
btn2.setOnClickListener(myClickLIstener);
btn2.setTag("btn2");

EDIT:

You are looking for ...

class superTop implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Log.e("", "onClick superTop");
        }

    }

    class NewClick extends superTop implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Log.e("", "onClick NewClick");

            super.onClick(v);

        }

    }

    findViewById(R.id.button1).setOnClickListener(new NewClick());

You can also call a listener ...

+3
source

OR SIMPLY

class YourClass implements OnClickListener{
    buttonOne.setOnclickListener(this);
    buttonTwo.setOnclickListener(this);
    buttonThree.setOnclickListener(this);

    public void onClick(View v){
        //do your stuff;
    }
}

Or, if you want, make your custom button:

Class Yourbutton extends Button implements Button.OnClickListener{
    public Yourbutton(Context c) {
        super(c);
    }
    public Yourbutton(Context c,AttributeSet s){
        super(c,s);
    }
    public void onClick(View v){
        //do your stuff;
    }
}

So, now you can use Yorkbutton as a common button that performs common tasks.

+3

, OnClickListener

button1.setOnClickListener(this);
button2.setOnClickListener(this);

onClick :

public void onClick(View view) {


    switch(view.getId()){

//here you need the id of the button i.e. its id in the xml file
    case R.id.button1:
        // do whatever you want button1 to do
        break;
    case R.id.button2:
        // do whatever you want button2 to do
        break;
    }
}
+2

android:onClick. onClick :

<Button
[...]
android:onClick="handleButton"/>

<Button
[...]
android:onClick="handleButton"/>

java :

public void handleButton(View v){
// do some stuff
}
+1

Listener, , , ,

View.OnClickListener yourClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            //play the music
        }
    };

button1.setOnClickListener(yourClickListener);
button2.setOnClickListener(yourClickListener);
button3.setOnClickListener(yourClickListener);
+1

, ,

<LinearLayout ...>
  <Button android:text="1" onClick="onButtonClicked" clickable="true" />
  <Button android:text="2" onClick="onButtonClicked" clickable="true" />
  <Button android:text="3" onClick="onButtonClicked" clickable="true" />
  <Button android:text="4" onClick="onButtonClicked" clickable="true" />
</LinearLayout>

, onClick , java- findViewById.

, ,

public void onButtonClicked(View v){
     // do whatever needs to be done. For example:
     Toast.makeText(getApplicationContext(), ((Button) v).getText() + " clicked", Toast.LENGTH_SHORT).show(); 
}

You do not need to repeat the same code for everyone. You can try the general receiver, for example:

private OnClickListener genericClickListener= new OnClickListener() { 
    public void Click(View v) {
            // do something
    } 
}; 

Then all you have to do is register all three buttons to use this genericClickListener. That is, inside onCreate(),

Button firstbtn  = (Button) findViewById(R.id.firstbtn); 
Button secondbtn = (Button) findViewById(R.id.secondbtn); 
Button thirdbtn  = (Button) findViewById(R.id.thirdbtn); 

firstbtn.setOnClickListener(genericClickListener);
secondbtn.setOnClickListener(genericClickListener);
thirdbtn.setOnClickListener(genericClickListener);

Third method

Create one function / method and onClick for all buttons calls the same Method/Function.

0
source

All Articles