Pregunta

I have a button placed in an ActionBar. How can I make a button listener which is shared between different layouts that includes the ActionBar?

¿Fue útil?

Solución 2

You can use a class like this:

public class Actionbar_BtnHandler extends Activity {
    Context context;
    public  Actionbar_BtnHandler (Context context)
    {
        this.context=context;
    }
    public void btn_handler (Button btn_mic,Button btn_post)
    {
        btn_mic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context,"MIKE",Toast.LENGTH_LONG).show();
            }
        });

        btn_post.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
    }

Otros consejos

Create a 'BaseActivity' or 'ParentActivity' or whatever you want to call it, which extends Activity. This simply does all things you do in every Activity.

Then all your other Activities, extend this ParentActivity, instead of the normal Activity.

Implement your actionbar creation and buttons which are always in it, and their actions in this ParentActivity.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top