搭建一个简单、易用的 Android 项目框架之BaseActivity(一)

来源:互联网 发布:c 网络编程教程 编辑:程序博客网 时间:2024/05/17 08:38

第一次写技术文章,写得不好望读者见谅吐舌头

因为 android 应用离不开 UI 并且这些界面大多都有一些共性,为了避免后面的实际开发过程中写大量重复且没有质量含的代码,就需要把这些界面的共性抽取出来,写在基类里面。

以下是 BaseActivity 的代码实现:

public abstract class TBaseActivity extends FragmentActivity implements View.OnClickListener {    protected Context mContext;//通用上下文    private Toast toast;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mContext = this;        toast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);        initViewAfterOnCreate();        initDataAfterOnCreate();    }    @Override    public void onClick(View v) {        if (v.getId() == R.id.iv_left) {            AnimUtils.toRightAnim(mContext);//左移动画关闭当前页面        }    }    protected <T> T $(int id) {        T view = (T) findViewById(id);        return view;    }    protected void registerOnClickListener(TBaseActivity mActivity, View... views) {//注册点击事件        for (View view : views) {            view.setOnClickListener(mActivity);        }    }    protected void showToast(String str) {        if (!TextUtils.isEmpty(str)) {            toast.setText(str);            toast.show();        }    }    protected void showToast(int strId) {        showToast(getString(strId));    }    protected void backWithTitle(String text) {        TextView tv = (TextView) findViewById(R.id.tv_title);        if (!TextUtils.isEmpty(text) && tv != null) {            tv.setText(text);        }        getLeftIV().setOnClickListener(this);    }    protected ImageView getLeftIV() {// 显示返回按钮并取得引用        ImageView iv = (ImageView) findViewById(R.id.iv_left);        if (iv != null) {            iv.setVisibility(View.VISIBLE);        }        return iv;    }    /**     * 用于从xml文件中inflate控件View     */    public abstract void initViewAfterOnCreate();    /**     * 给View填充数据     */    public abstract void initDataAfterOnCreate();}

注:(1)为了保持每个页面的子 Activity 保持近似的书写格式 将TBaseActivity 定义为抽象类,并添加相应的抽象方法(子类必须实现)

   (2)findViewById()这种没的技术含却又不得不写的方法几乎没办法避免,除非使用注解框架(能节省一定的代码,但会导致可读性降低),但我们可以简化这个方法的实现方式,通过 protected<T> T $(intid) 泛型转换类型,$精减方法名称,可能有效的减少代码量。

   (3)虽然有人喜欢在 BaseActivity 里面添加对TitleBar 的处理,但我还是习惯 TitleBar 在必要的时候按需引入,通过 backWithTitle()方法对 标准的 TitleBar 进行设置,并且在基类里实现 TitleBar 返回的 点击事件。

   (4)很多时候需要使用 Toast(实际上我是把 Toast 写成单例形式,但这里为了演示,故暂写在这里) 给用户展示一些提示信息,为了方便把 toast 的展示写成方法,节省代码量。


下面是一个简单的页面包含标题栏,返回按钮(及相应关闭事件),测试按钮(点击展示 Toast 信息)



上面页面的 Java 代码如下


public class TestActivity extends TBaseActivity {    private TextView tv_test;    @Override    protected void onCreate(Bundle savedInstanceState) {        setContentView(R.layout.act_test);        super.onCreate(savedInstanceState);    }    @Override    public void initViewAfterOnCreate() {        tv_test = $(R.id.tv_test);        registerOnClickListener(this, tv_test);//注册点击事件        backWithTitle("测试标题");    }    @Override    public void initDataAfterOnCreate() {    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.tv_test://点击按钮                showToast(tv_test.getText().toString());                break;        }        super.onClick(v);    }}
注:为了确保 initViewAfterOnCreate()  initDataAfterOnCreate() 这个个方法的正常执行,子类只需要把 setContentView(R.layout.act_test);放在 super.onCreate(savedInstanceState);之前。

页面的布局文件(act_test)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    android:orientation="vertical">    <include layout="@layout/title_bar"/>    <TextView        android:id="@+id/tv_test"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/white"        android:gravity="center"        android:text="点击我,展示 Toast"        android:textSize="20dp"/></LinearLayout>

标题栏布通用局文件(title_bar)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@color/white"    android:clickable="true"    android:gravity="center_vertical"    android:orientation="vertical">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="@dimen/title_bar_height"        android:gravity="center_vertical">        <ImageView            android:id="@+id/iv_left"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_gravity="start"            android:background="@drawable/btn_white_selector"            android:paddingLeft="@dimen/padding_normal"            android:paddingRight="@dimen/padding_larger"            android:src="@drawable/img_back"            android:visibility="gone"/>        <TextView            android:id="@+id/tv_title"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_gravity="center"            android:paddingBottom="@dimen/padding_normal"            android:paddingTop="@dimen/padding_normal"            android:singleLine="true"            android:text="@string/app_name"            android:textColor="@color/text_normal"            android:textSize="@dimen/big_text"/>    </FrameLayout>    <TextView        style="@style/normal_line"/></LinearLayout>


注:截止以上就实现了一个精减的 Activity 页面,当然实际开发过程基类有更多的方法,但为了文件演示,暂时的基类写得比较简单,随续文章的的更多功能的实现,会一步步的把相应的必要方法加上去。

Demo GitHub 地址:https://github.com/chende008/TestProject

0 0