一、返回键处理:
Android返回键我们经常都要进行处理,分别有以下两个不同的处理方式
方式一:按返回键显示退出提示框( 自定义提示框架可以参考[Android使用自定义AlertDialog(退出提示框) ] )
方式二:按返回键不退出应用,返回主页面(即与按Home键操作一样)
方式一:
public class WelcomeActivity extends Activity {
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 如果是返回键,直接返回到桌面
if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
showExitGameAlert();
}
return super.onKeyDown(keyCode, event);
}
private void showExitGameAlert() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
window.setContentView(R.layout.shrew_exit_dialog);
ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
exitApp();
}
});
ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dlg.cancel();
}
});
}
}
方式二:
public class SenseSoccerScoreActivity extends Activity {
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 如果是返回键,直接返回到桌面
// 经过测试,如果是乐Phone返回桌面会报错
if(keyCode == KeyEvent.KEYCODE_BACK){
// 创建退出系统提示框
if(notSupportKeyCodeBack()){
new AlertDialog.Builder(this)
.setMessage(this.getText(R.string.sure_exit_app).toString())
.setPositiveButton(R.string.text_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
exitApp(); // 退出应用处理
}
})
.setNegativeButton(R.string.text_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
} else {
// 返回桌面,经测试,有一些手机不支持,查看 notSupportKeyCodeBack 方法
Intent i= new Intent(Intent.ACTION_MAIN);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
return false;
}
}
return super.onKeyDown(keyCode, event);
}
// 经过测试,如果是乐Phone返回桌面会报错
private boolean notSupportKeyCodeBack(){
if("3GW100".equals(Build.MODEL)
|| "3GW101".equals(Build.MODEL)
|| "3GC101".equals(Build.MODEL)) {
return true;
}
return false;
}
}
http://www.apkbus.com/forum.php?mod=viewthread&tid=27607&reltid=27605&pre_pos=1&ext=CB
二、程序退出对话框:
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 如果是返回键,直接返回到桌面
if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
showExitGameAlert();
}
return super.onKeyDown(keyCode, event);
}
private void showExitGameAlert() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
// *** 主要就是在这里实现这种效果的.
// 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
window.setContentView(R.layout.shrew_exit_dialog);
// 为确认按钮添加事件,执行退出应用操作
ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
exitApp(); // 退出应用...
}
});
// 关闭alert对话框架
ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dlg.cancel();
}
});
}
文件名为 : shrew_exit_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<!-- 退出游戏的背景图 -->
<ImageView android:id="@+id/exitGameBackground"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/bg_exit_game" />
<!-- 确认按钮 -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
android:layout_alignLeft="@+id/exitGameBackground"
android:layout_marginBottom="30dp"
android:layout_marginLeft="35dp"
android:id="@+id/btn_ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn_ok" />
<!-- 取消按钮 -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
android:layout_alignRight="@+id/exitGameBackground"
android:layout_marginBottom="30dp"
android:layout_marginRight="35dp"
android:id="@+id/btn_cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn_cancel" />
</RelativeLayout>
http://www.r-base.net/archives/134
===============================
但是在退出的时候跑出了WindowLeaked异常。Activity 的生命周期与 Dialog 的不一样,AlertDialog.Builder builder=new AlertDialog.Builder(context),这个context 是Activity的上下文,Dialog 要在Activity 销毁前 dismiss()掉,不然就窗体溢出。
正确的做法是在 showExitGameAlert();后在调用finish();结束Activity:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 如果是返回键,直接返回到桌面
if (keyCode == KeyEvent.KEYCODE_BACK
|| keyCode == KeyEvent.KEYCODE_HOME) {
showExitGameAlert();
finish();
}
return super.onKeyDown(keyCode, event);
}