美文网首页实用控件安卓技巧app开发
封装Activity,并在具体场景中实现!

封装Activity,并在具体场景中实现!

作者: 脑袋君 | 来源:发表于2016-08-29 00:15 被阅读753次

        现在开发中为了提升开发的效率和代码的高效利用,我们更愿意将经常用的方法封装起来,更方便的服务开发过程,所有今天给大家带来一个Activity封装,并在具体场景中实现!

    这里我们进行封装方法:

         1.自定义toolbar

         2.Intent启动

         3.Toast

         4. show ProgressDialog

         5.hide ProgressDialog

         6.click menu and event

            .......

    下面贴出代码:

    这里我们需要用到java的回收机制: 主要是用来对anctivity的回收

    public class ActivityStackManager {

    private staticVectoractivityStack=newVector<>();

    public static voidadd(BaseActivity activity) {

    activityStack.add(activity);

    }

    public static voidremove(BaseActivity activity) {

    activityStack.remove(activity);

    }

    public static voidclear() {

    activityStack.clear();

    }

    public staticBaseActivitygetLast() {

    returnactivityStack.lastElement();

    }

    public static intgetSize() {

    returnactivityStack.size();

    }

    public static voidexit() {

    for(BaseActivity activity :activityStack) {

    if(activity !=null) {

    activity.finish();

    }

    }

    clear();

    }

    下面我们具体看看封装:

    public class BaseActivity extends AppCompatActivity {

    protected Toolbar mToolbar;

    protected TextView toolbar_title;

    protected ImageView right_buttom;

    protected Toast mToast;

    protected ProgressDialog bar;

    @Override

    protected voidonCreate(@NullableBundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    //这里采用 弱引用

    WeakReference weekReferece =newWeakReference<>(this);

    ActivityStackManager.add(weekReferece.get());

    }

    protected  void init() {

    mToolbar= (Toolbar) findViewById(R.id.toolbar);

    toolbar_title= (TextView) findViewById(R.id.tv_toolbar_title);

    right_buttom= (ImageView) findViewById(R.id.btn_right);

    if(mToolbar!=null) {

    mToolbar.setNavigationIcon(R.drawable.left_jiantou);

    toolbar_title.setText("");

    setSupportActionBar(mToolbar);

    }

    }

    @Override

    protected voidonResume() {

    super.onResume();

    MobclickAgent.onResume(this);

    }

    @Override

    protected voidonPause() {

    super.onPause();

    MobclickAgent.onPause(this);

    }

    @Override

    protected voidonDestroy() {

    super.onDestroy();

    ActivityStackManager.remove(this);

    }

    封装Intent,但是需要携带数据还是需要重新写intent

    public void go(Class cls) {

    startActivity(newIntent(this,cls));

    }

    //String ----> text show

    public void showToast(CharSequence text) {

    if(mToast==null) {

    mToast= Toast.makeText(this,text,Toast.LENGTH_LONG);

    }

    mToast.setText(text);

    mToast.show();

    }

    //getResources -----> text show

    public void showToast(@StringResintres) {

    showToast(getResources().getText(res));

    }

    //show ProgressDialog

    public voidshowProgress(String msg) {

    if(bar==null) {

    bar=newProgressDialog(this);

    bar.setMessage(msg);

    bar.setIndeterminate(true);

    bar.setCancelable(false);

    }

    if(bar.isShowing()) {

    bar.setMessage(msg);

    }

    bar.show();

    }

    //hide ProgressDialog

    public voiddismissDialog() {

    if(bar!=null&&bar.isShowing()) {

    bar.setCancelable(false);

    bar.dismiss();

    }

    }

    //click menu and event

    public booleanonOptionsItemSelected(MenuItem item) {

    if(item.getItemId() == android.R.id.home){

    finish();

    return true;

    }

    return super.onOptionsItemSelected(item);

    }

    下面我们就来继承BaseActivity,实现这些封装的方法

    public class StarActivity  extends BaseActivity {

    @Override

    protected voidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_star);

    init();

    }

    @Override

    protected voidinit() {

    super.init();

    toolbar_title.setText("我是一个toobar title");

    right_buttom.setVisibility(View.VISIBLE);

    right_buttom.setImageResource(R.drawable.right_hui);

    right_buttom.setOnClickListener(newView.OnClickListener() {

    @Override

    public voidonClick(View view) {

    showProgress("正在加载中...");

    newThread(newRunnable() {

    @Override

    public voidrun() {

    try{

    sleep(2000);

    }catch(InterruptedException e) {

    e.printStackTrace();

    }

    }

    }).start();

    }

    });

    findViewById(R.id.btn_showToast).setOnClickListener(newView.OnClickListener() {

    @Override

    public voidonClick(View view) {

    showToast("Click Me Show Toast");

    }

    });

    findViewById(R.id.btn_goIntent).setOnClickListener(newView.OnClickListener() {

    @Override

    public voidonClick(View view) {

    go(StarTwoActivity.class);

    }

    });

    }

    上面只是贴出大概的代码,其他简单的就不贴出来!

    相关文章

      网友评论

      • Aeatho:额。没什么参考价值

      本文标题:封装Activity,并在具体场景中实现!

      本文链接:https://www.haomeiwen.com/subject/jfvesttx.html