根据已上线的app里总结出来的实用小技巧
BaseActivity基类的使用, 还有很多例如:BaseFragment、BaseService等等就不一一列举了
每个app必备(可以根据自己需求来自己定义, 或增加)#####
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
/** 当前Activity名字 */
protected static String ACTIVITY_NAME;
protected Bundle savedInstanceState;
public Context context;
public MissBearApplication application;
/** 屏幕宽高, 状态栏高 */
public int screenWidth, screenHeight, statusBarHeightX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstanceState;
LogUtils.i(ACTIVITY_NAME = getClass().getSimpleName());
context = this;
application = (MissBearApplication) getApplication();
screenWidth = ScreenManager.getScreenSize(context)[0];
screenHeight = ScreenManager.getScreenSize(context)[1];
statusBarHeightX = ScreenManager.getStatusBarHeight(context);
ActivityCollector.addActivity(this);
setContentView();
initData();
initView();
initListener();
}
@Override
public void onBackPressed() {
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
isActivityFinish = true;
ActivityCollector.removeActivity(this);
finish();
}
public Toast toast;
public void showToast(String text) {
if (toast == null) {
toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
} else {
toast.setText(text);
}
toast.show();
}
public void showToast(BaseActivity activity, String text) {
if (toast == null) {
toast = Toast.makeText(activity, text, Toast.LENGTH_SHORT);
} else {
toast.setText(text);
}
toast.show();
}
/**
* 显示带图片的Toast
* @param text 需要显示的文字
* @param imgId 需要显示的图片
*/
public void showImageToast(String text, int imgId) {
Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
toast.setText(text);
LinearLayout toastView = (LinearLayout) toast.getView();
toastView.setGravity(Gravity.CENTER);
ImageView toastImg = new ImageView(context);
toastImg.setLayoutParams(new LinearLayout.LayoutParams(dp2px(25), dp2px(25)));
toastImg.setScaleType(ImageView.ScaleType.FIT_XY);
toastImg.setImageResource(imgId);
toastView.addView(toastImg, 0);
toast.show();
}
public void showToast(int textId) {
showToast(getString(textId));
}
public BaseActivity getActivity() {
return this;
}
public Intent getIntent(Class clazz) {
return new Intent(context, clazz);
}
public void startIntent(Class clazz) {
startActivity(getIntent(clazz));
}
public int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
context.getResources().getDisplayMetrics());
}
protected void hideStatusBar() {
int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;//隐藏状态栏, 定义全屏参数
Window window = getWindow(); //获得当前窗体对象
window.setFlags(flag, flag);//设置当前窗体为全屏显示
}
private long lastClickTime;
/**
* 实现连续点击两次才退出应用程序
*/
public void exit() {
if ((System.currentTimeMillis() - lastClickTime) > 2000) {
showToast("再按一次退出" + context.getResources().getString(R.string.app_name));
lastClickTime = System.currentTimeMillis();
} else {
ActivityCollector.removeAll();
System.exit(0);
}
}
@Override
public void onClick(View v) {
//继承了BaseActivity的类, 如果要使用返回关闭Activity的功能
//需要在继承的Activity的onClick(View v)里写上super.onClick(v);
switch (v.getId()) {
case R.id.ll_back:
//R.id.back为标题左上角的返回控件
onBackPressed();
break;
default:
break;
}
}
public abstract void setContentView();
public abstract void initView();
public abstract void initListener();
public abstract void initData();
}
使用方式#####
public class UpdateNickNameActivity extends BaseActivity implements View.OnClickListener {
private LinearLayout ll_clear_nickname;
@Override
public void setContentView() {
setContentView(R.layout.activity_update_nickname);
}
@Override
public void initView() {
//控件初始化
ll_clear_nickname = (LinearLayout) findViewById(R.id.ll_clear_nickname );
}
@Override
public void initListener() {
//监听器设置
ll_clear_nickname.setOnClickListener(this);
}
@Override
public void initData() {
//数据初始化
}
public void onClick(View v) {
super.onClick(v);
switch (v.getId()) {
case R.id.ll_clear_nickname:
//其他功能
break;
}
}
}