BaseActivity 打开新的activity 新的activityForResult dip、px互换 toast打印 sp为字体大小单位时设置字体不随系统变化而变化封装类

BaseActivity

  • 我们在activity中经常打开新的activity(openActivity)
  • 我们打开新的activityForResult(openActivityForResult)
  • 我们需要的dip、px互换(dip2px、px2dip)
  • 我们需要toast打印(showToastSafe)
  • 部分需要,在使用sp为字体大小单位时设置字体不随系统变化而变化(重写getResources方法)


public class BaseActivity extends InitActivity {

/** UI 线程ID */
private long mUIThreadId;
protected LoveApplication app;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (LoveApplication) getApplication();
}

@Override
protected void onDestroy() {
super.onDestroy();
}


/**
* 通过类名启动Activity
*
* @param pClass
*/
protected void openActivity(Class pClass) {
openActivity(pClass, null);
}

protected void openActivity(Class pClass, String key, String value) {
Intent intent = new Intent(this, pClass);
intent.putExtra(key, value);
startActivity(intent);
}

/**
* 通过类名启动Activity,并且含有Bundle数据
*
* @param pClass
* @param pBundle
*/
protected void openActivity(Class pClass, Bundle pBundle) {
Intent intent = new Intent(this, pClass);
if (pBundle != null) {
intent.putExtras(pBundle);
}
startActivity(intent);
}

public void openActivityForResult(Class pClass, String key, String value, int requestCode) {
Intent intent = new Intent(this, pClass);
intent.putExtra(key, value);
openActivityForResult(pClass, null, requestCode);
}

public void openActivityForResult(Class pClass, int requestCode) {
openActivityForResult(pClass, null, requestCode);
}

protected void openActivityForResult(Class pClass, Bundle pBundle, int pRrequestCode) {
Intent intent = new Intent(this, pClass);
if (pBundle != null) {
intent.putExtras(pBundle);
}
startActivityForResult(intent, pRrequestCode);
}


/** dip转换px */
public int dip2px(int dip) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
}

/** pxz转换dip */
public int px2dip(int px) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (px / scale + 0.5f);
}

/** 对toast的简易封装。线程安全,可以在非UI线程调用。 */
public void showToastSafe(final int resId, final int duration) {
if (Process.myTid() == mUIThreadId) {
// 调用在UI线程
Toast.makeText(getBaseContext(), resId, duration).show();
} else {
// 调用在非UI线程
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(), resId, duration).show();
}
});
}
}

/** 对toast的简易封装。线程安全,可以在非UI线程调用。 */
public void showToastSafe(final CharSequence text, final int duration) {
if (Process.myTid() == mUIThreadId) {
// 调用在UI线程
Toast.makeText(getBaseContext(), text, duration).show();
} else {
// 调用在非UI线程
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getBaseContext(), text, duration).show();
}
});
}
}


/*
* 设置字体不随系统变化而变化
*/
@Override
public Resources getResources() {
Resources res = super.getResources();
Configuration config=new Configuration();
config.setToDefaults();
if(res != null){
res.updateConfiguration(config,res.getDisplayMetrics() );
}
return res;
}

}

你可能感兴趣的:(BaseActivity 打开新的activity 新的activityForResult dip、px互换 toast打印 sp为字体大小单位时设置字体不随系统变化而变化封装类)