public abstract class BaseActivity extends AppCompatActivity { /***是否显示标题栏*/ private boolean isshowtitle = false; /***是否显示标题栏*/ private boolean isshowstate = false; /***封装toast对象**/ private static Toast toast; /***获取TAG的activity名称**/ protected final String TAG = this.getClass().getSimpleName(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!isshowtitle) { requestWindowFeature(Window.FEATURE_NO_TITLE); } if (isshowstate) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } //设置布局 setContentView(intiLayout()); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setSubtitle(setToolbarTitle()); toolbar.setNavigationIcon(setToolbarIco()); toolbar.setSubtitleTextColor(setToolbarTextColor()); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); Intent intent = getIntent(); getIntent(intent); //初始化控件 initView(); //设置数据 initData(); } public abstract String setToolbarTitle(); public abstract Drawable setToolbarIco(); public abstract int setToolbarTextColor(); /** * 获取Intent */ public abstract void getIntent(Intent intent); /** * 设置布局 * * @return */ public abstract int intiLayout(); /** * 初始化布局 */ public abstract void initView(); /** * 设置数据 */ public abstract void initData(); /** * 是否设置标题栏 * * @return */ public void setTitle(boolean ishow) { isshowtitle = ishow; } /** * 设置是否显示状态栏 * * @param ishow */ public void setState(boolean ishow) { isshowstate = ishow; } /** * 显示长toast * * @param msg */ public void toastLong(String msg) { if (null == toast) { toast = new Toast(this); toast.setDuration(Toast.LENGTH_LONG); toast.setText(msg); toast.show(); } else { toast.setText(msg); toast.show(); } } /** * 显示短toast * * @param msg */ public void toastShort(String msg) { if (null == toast) { toast = new Toast(this); toast.setDuration(Toast.LENGTH_SHORT); toast.setText(msg); toast.show(); } else { toast.setText(msg); toast.show(); } }
自己封装的BaseActivity和BaseFragment 可以实现基本的功能,后期会不断根据需求加api
最新推荐文章于 2019-10-25 16:13:11 发布