第一次寫技術文章,寫得不好望讀者見諒
因爲 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>
Demo GitHub 地址:https://github.com/chende008/TestProject