简体   繁体   中英

How to declare common Listener for buttons click event?

In android app coded for common method for all buttons click event, here is the code,

public void onCreate(Bundle  savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filter);
        btnOne = (Button)findViewById(R.id.btnone);
        btnTwo = (Button)findViewById(R.id.btntwo);
        btnThree = (Button)findViewById(R.id.btnthree);
        btnFour = (Button)findViewById(R.id.btnfour);
        btnFive = (Button)findViewById(R.id.btnfive);
        btnSix = (Button)findViewById(R.id.btnsix);
        btnSeven = (Button)findViewById(R.id.btnseven);
        btnEight = (Button)findViewById(R.id.btneight);
        btnNine = (Button)findViewById(R.id.btnnine);
        btnTen = (Button)findViewById(R.id.btnten);

    OnClickListener listener = new OnClickListener()
    {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            doAction(v);
        }

    };

}

public void doAction(View v)
    {
        Object tagObject = v.getTag();
        int tag = (Integer) v.getTag();
        String val = (String) d.get(tag);
        if(val.equals("off"))
        {
            //select(tagObject);
            //d.put(tag, "on");

        Toast.makeText(getBaseContext(), "Button"+tag+"select", Toast.LENGTH_LONG).show();
    }
    else if(val.equals("on"))
    {
        //unSelect(tagObject);
        //d.put(tag, "off");
        Toast.makeText(getBaseContext(), "Button"+tag+"unselect", Toast.LENGTH_LONG).show();
    }   
}

This code is not working for me. Please give any idea....... Thanks in advance

How about declaring your listener first and then calling setOnClickListener on your views :

OnClickListener listener = new OnClickListener()
{

    public void onClick(View v) {
        // TODO Auto-generated method stub
        doAction(v);
    }

};
btnOne.setOnClickListener(listener);
btnTwo.setOnClickListener(listener);
...

You have declared the listener but you forgot to set the listener for each button. Do this for all buttons : btnOne.setOnClickListener(listener);

You need to set the listener in the button..

public void onCreate(Bundle  savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.filter);
    ...

OnClickListener listener = new OnClickListener()
{

    public void onClick(View v) {
        // TODO Auto-generated method stub
        doAction(v);
    }

};

btnOne.setOnClickListener();
btnTwo = (Button)findViewById(R.id.btntwo);
    ...

}

Have your class implement View.OnClickListener , like

public class MyActivity extends Activity implements View.OnClickListener {

    Button button1, button2, button3;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate();

        ...

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

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

   @Override
   public void onClick(View v) {
       switch(v.getId()) {
           case R.id.button1:
           // do stuff;
           break;
           case R.id.button2:
           // do stuff;
           break;
       ...
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM