public class BaseActivity extends AppCompatActivity { //让返回键起作用 @Override public boolean onOptionsItemSelected(MenuItem item) { Log.i("info","执行BaseActivity中的onOptionsItemSelected"); if (android.R.id.home == item.getItemId()) { this.finish(); //关闭当前接口 return true; } return super.onOptionsItemSelected(item); } public void getActionBar(String title, AppCompatActivity activity) { // 设置标题栏返回键 ActionBar callbackBar = activity.getSupportActionBar(); if (callbackBar != null) { callbackBar.setDisplayHomeAsUpEnabled(true);//给左上角添加一个返回的箭头图标 callbackBar.setHomeButtonEnabled(true);//设置图标可以点击 callbackBar.setDisplayShowHomeEnabled(true);//使图标可以显示 callbackBar.setTitle(title); //标题设置 } } protected void showMessage(String message) { String title = Constant.appName; String negativeBtnText = Constant.btnTextOK; // 绑定失败,提示后退出应用 AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); alertDialog.setTitle(title); alertDialog.setMessage(message); alertDialog.setNegativeButton(negativeBtnText, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); alertDialog.setCancelable(false); alertDialog.show(); } }
//继承BaseActivity直接调用方法。
public class IDCardOperationActivity extends BaseActivity implements View.OnClickListener { yhid15reader yhidReader = new yhid15reader(); @Override protected void onCreate(Bundle savedInstanceState) { //创建静态内部类 super.onCreate(savedInstanceState); //当前窗口设置返回按键 getActionBar("身份证模块", this); } //调用消息弹框。 public void show(View v) { showMessage("Open 成功"); }
}