using common Layout and its event in all activities

后端 未结 5 1437
你的背包
你的背包 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: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
        }
    }
    

提交回复
热议问题