可以帮我写一个功能齐全的android的baseActivity抽象类
时间: 2023-05-12 17:04:01 浏览: 81
当然可以,以下是一个功能齐全的android的baseActivity抽象类的代码:
```java
public abstract class BaseActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
initView();
initData();
}
protected abstract int getLayoutId();
protected abstract void initView();
protected abstract void initData();
protected void showProgressDialog(String message) {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setCancelable(false);
}
mProgressDialog.setMessage(message);
mProgressDialog.show();
}
protected void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
protected void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
protected void startActivity(Class<?> cls) {
startActivity(new Intent(this, cls));
}
protected void startActivity(Class<?> cls, Bundle bundle) {
Intent intent = new Intent(this, cls);
intent.putExtras(bundle);
startActivity(intent);
}
protected void startActivityForResult(Class<?> cls, int requestCode) {
startActivityForResult(new Intent(this, cls), requestCode);
}
protected void startActivityForResult(Class<?> cls, int requestCode, Bundle bundle) {
Intent intent = new Intent(this, cls);
intent.putExtras(bundle);
startActivityForResult(intent, requestCode);
}
}
```
这个抽象类包含了常用的一些方法,比如显示进度对话框、隐藏进度对话框、显示Toast、启动Activity等等。你可以根据自己的需要进行修改和扩展。
阅读全文