public abstract class BaseActivity extends Activity implements View.OnClickListener {
protected String TAG;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
TAG =this.getClass().getSimpleName();
mProgressDialog =new ProgressDialog(this);
initView();
initData();
initListen();
}
protected abstract int getLayoutId();
protected abstract void initView();
protected abstract void initData();
protected abstract void initListen();
protected void initTitle(int leftStr, int leftRes, int titleStr, int rightStr, int rightRes) {
findViewById(R.id.title_left).setOnClickListener(this);
findViewById(R.id.title_right).setOnClickListener(this);
if (leftStr !=0) {
TextView tvLeft = findViewById(R.id.tv_title_left);
tvLeft.setText(getString(leftStr));
}
if (leftRes !=0) {
ImageView ivLeft = findViewById(R.id.iv_title_left);
ivLeft.setImageResource(leftRes);
}
if (titleStr !=0) {
TextView tvTitle = findViewById(R.id.tv_title);
tvTitle.setText(getString(titleStr));
}
if (rightStr !=0) {
TextView tvRight = findViewById(R.id.tv_title_right);
tvRight.setText(getString(rightStr));
}
if (rightRes !=0) {
ImageView ivRight = findViewById(R.id.iv_title_right);
ivRight.setImageResource(rightRes);
}
}
protected void finishThis() {
finish();
}
protected void goOtherActivity(Class target, boolean isFinishThis) {
startActivity(new Intent(this, target));
if (isFinishThis) {
finishThis();
}
}
protected void showProgress(String title,String content){
mProgressDialog.setTitle(title);
mProgressDialog.setMessage(content);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
}
protected void cancelProgress(){
if (mProgressDialog.isShowing()){
mProgressDialog.cancel();
}
}
@Override
protected void onDestroy() {
cancelProgress();
super.onDestroy();
}
//--------------设置让ET失去焦点的方法-----------Start
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() ==MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
hideInputMethod(this, v);
}
}
return super.dispatchTouchEvent(ev);
}
public static boolean isShouldHideInput(View v, MotionEvent event) {
if (v !=null && (vinstanceof EditText)) {
int[]leftTop = {0, 0};
v.getLocationInWindow(leftTop);
int left =leftTop[0], top =leftTop[1], bottom =top + v.getHeight(), right =left
+ v.getWidth();
return !(event.getX() >left && event.getX()
&& event.getY() >top && event.getY()
}
return false;
}
public static boolean hideInputMethod(Context context, View v) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
return imm !=null &&imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
//--------------设置让ET失去焦点的方法-----------End
}