註解與抽取基類

Castertonone發表於2018-06-11

1.首先要在專案的build裡
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
}

其次新增依賴
compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
在最上新增
apply plugin: 'com.jakewharton.butterknife'
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
2.封裝的
 
public class MainActivity extends BaseActivity {

    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_main;
    }
    @OnClick({R.id.btn1,R.id.btn2})
    public void a(View view){
        switch (view.getId()){
            case R.id.btn1:
                showToast("btn1");
                break;
            case R.id.btn2:
                showToast("btn2");
                break;
        }
    }
}

2.主方法

 
public abstract class BaseActivity extends AppCompatActivity{
    private final String TAG = BaseActivity.class.getSimpleName();
    protected Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayoutId());
      unbinder = ButterKnife.bind(this);
        initView();
        initData();
    }

    /**
     * 初始化view
     */
    protected abstract void initView();
    /**
     * 初始化資料
     */
    protected abstract void initData();
    /**
     * 根佈局id
     * @return
     */
    protected abstract int bindLayoutId();
    /**
     * 顯示toast
     * @param msg
     */
    public void showToast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
    /**
     * 無值傳遞跳轉
     * @param cls
     */
    public void startActivity(Class<? extends Activity> cls){
        Intent intent = new Intent(this,cls);
        startActivity(intent);
    }
    /**
     * 有值傳遞跳轉
     * @param cls
     */
    public void startActivity(Bundle bundle,Class<? extends Activity> cls){
        Intent intent = new Intent(this,cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }
    /**
     * 是否
     * @param flag
     */
    public void isFullScreen(boolean flag){
        if(flag){
            // TODO: 2018/6/7  全屏的程式碼
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }else {
            // TODO: 2018/6/7  fei
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(unbinder != null){
            unbinder.unbind(); //解綁
        }
    }
}

3.5佈局

 
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

相關文章