- public class BaseActivity extends Activity {
- Resources res; // 通用资源缩写
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示标题
- super.onCreate(savedInstanceState);
- res = getResources(); // 通用资源缩写
- // 优化输入法模式
- int inputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
- getWindow().setSoftInputMode(inputMode);
- }
- /**
- * 检查字符串是否是空对象或空字符串
- *
- * @param str
- * @return 为空返回true,不为空返回false
- */
- public boolean isNull(String str) {
- if (null == str || "".equals(str) || "null".equalsIgnoreCase(str)) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 检查字符串是否是字符串
- *
- * @param str
- * @return 为空返回true,不为空返回false
- */
- public boolean isStr(String str) {
- return !isNull(str);
- }
- /**
- * 从当前activity跳转到目标activity,<br>
- * 如果目标activity曾经打开过,就重新展现,<br>
- * 如果从来没打开过,就新建一个打开
- *
- * @param cls
- */
- public void gotoExistActivity(Class<?> cls) {
- Intent intent;
- intent = new Intent(this, cls);
- intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
- startActivity(intent);
- }
- /**
- * 新建一个activity打开
- *
- * @param cls
- */
- public void gotoActivity(Class<?> cls) {
- Intent intent;
- intent = new Intent(this, cls);
- startActivity(intent);
- }
- /**
- * 通用消息提示
- *
- * @param resId
- */
- public void toast(int resId) {
- Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
- }
- /**
- * 通用消息提示
- *
- * @param resId
- */
- public void toast(String msg) {
- Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
- }
- /**
- * 从资源获取字符串
- *
- * @param resId
- * @return
- */
- public String getStr(int resId) {
- return res.getString(resId);
- }
- /**
- * 从EditText 获取字符串
- *
- * @param editText
- * @return
- */
- public String getStr(EditText editText) {
- return editText.getText().toString();
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_BACK:
- this.finish();
- }
- return super.onKeyDown(keyCode, event);
- }
- }