문제

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?

도움이 되었습니까?

해결책 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

            }
        });
    }

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top