using common Layout and its event in all activities

后端 未结 5 1394
你的背包
你的背包 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
    
    0 讨论(0)
  • 2021-01-16 11:12

    yes,You can make one activity say HeaderActivity

    in that onCreate setContentView(R.layout.header); set header.xml

    and write your all Button Click events in this class once and

    and now in other Activities extend HeaderActivity. like,

    public class SecondActivity extends HeaderActivity
    {
    }
    

    To show different layouts ..

    put a LinearLayout in header.xml below that buttons and in each other activities use

    ViewGroup vg = (ViewGroup) findViewById(R.id.lldata);
    ViewGroup.inflate(getApplicationContext(), R.layout.listwitter, vg);
    

    Here lldata is LinearLayout in header.xml

    to show different layout in diff activity.

    0 讨论(0)
  • 2021-01-16 11:21

    Hello Bindalbhai..

    MainActivity myAct = (MainActivity) this.getParent();
    
    TextView myTitleText = (TextView)myAct.findViewById(R.id.txtTitle);
    

    It may be helps you. Or you can use ViewGroup for this.

    Thanks.

    0 讨论(0)
  • 2021-01-16 11:37

    You should be able to use basic OOP strategies like you said. Create a parent activity that handles the onClick events. Then all your activities should extend that parent. They will automatically have access to the onClick events as long as the methods are not private.

    If you need to do different things onClicks in each activity, it might be worth approaching this differently, and or using a call back style. Example of the basic structure:

    public class ParentActivity extends Activity {
        public void onMyButtonClick(View v) {
            // do your thing
        }
    }
    
    public class ChildActivity extends ParentActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // your activity
        }
    }
    
    0 讨论(0)
  • 2021-01-16 11:37
    1. Define a header layout with 4 buttons and define android:onClick attributes inside xml layout for e.g. android:onClick="btnHomeClick", android:onClick="btnSearchClick"....etc.

    2. Define an Abstract class by extending Activity and implement all these click method:

      public void btnHomeClick(View v) {
      }
      
      public void btnSearchClick(View v)
      {
      }
      
    3. include that header layout by tag in your xml layout files. Now extends the above activity class in your every activity class.

    4. this way you just has to define a click event for once.

    0 讨论(0)
提交回复
热议问题