android 简单工具类:BaseActivity Toast

1.BaseActivity的设计实现:
可以为其他activity提供便利工具
 
     
public abstract class BaseActivity extends Activity {
 
protected String TAG;
 
protected BaseApplication application;
protected SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
TAG = this.getClass().getSimpleName();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
application = (BaseApplication) getApplication();
sp = getSharedPreferences(CommonConstants.SP_NAME, MODE_PRIVATE);
}
protected void intent2Activity(Class extends Activity> tarActivity) {
Intent intent = new Intent(this, tarActivity);
startActivity(intent);
}
protected void showToast(String msg) {
ToastUtils.showToast(this, msg, Toast.LENGTH_SHORT);
}
 
protected void showLog(String msg) {
Logger.show(TAG, msg);
}
 
}
2.Toast自定义的实现:可以解决toast接连弹出的问题:
 
      
public class ToastUtils {
private static Toast mToast;
/**
* 显示Toast
*/
public static void showToast(Context context, CharSequence text, int duration) {
if(mToast == null) {
mToast = Toast.makeText(context, text, duration);
} else {
mToast.setText(text);
mToast.setDuration(duration);
}
mToast.show();
}
 
}

你可能感兴趣的:(java)