1.多个Activity共用相同布局或者相同控件:避免重复代码
BaseActivit代码:
public class BaseActivity extends Activity implements OnClickListener{ /* * 多个Activity共用相同布局或者相同控件 * 写个基类继承,避免重复代码 */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.common_titlebar_btn_back: finish(); break; default: break; } baseOnClick(v); } protected void baseOnClick(View v) { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// setContentView(R.layout.activity_base); } @Override protected void onResume() { super.onResume(); onPostOnCreate(); } public final void onPostOnCreate()//find同一个控件 { findViewById(R.id.common_titlebar_btn_back).setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.base, menu); return true; }}
子类代码:
public class ThirdActivity extends BaseActivity{ /** * layout里复用同一布局: **/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third_main); }}