using common Layout and its event in all activities

后端 未结 5 1395
你的背包
你的背包 2021-01-16 11:10

I have one common layout that has four button at top bar and i am using this layout in all activities by including that common layout in all activities layout by

5条回答
  •  醉梦人生
    2021-01-16 11:11

    Create a static object in order to listen to the button clicks.Based on this object handle events in all activities.

    For example say Constants.class:

    public class Constants {
        private static int buttonState1;
            public static int getButtonState(){
            return buttonState1;
        }
        public static int  setButtonState(int x){
            buttonState1=x;
        }
    
    
    }
    

    And in your activity:

    Button bt1=(Button)  findViewById(R.id.bt1);
    
    bt1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                                       Constants.setButtonState(1);
                             }
                });
    

    And in other activities

    if(Constants.getButtonState()==1)
                    //do something
    

提交回复
热议问题