3

我想知道当用户尝试退出应用程序时你会如何出现警告?所以这包括他们是否也按下后退按钮。最好的方法是什么?

我已经在一些主流游戏上看到过这样做。

4

2 回答 2

7

您可以在子类中添加onBackPressed()Application拦截后退按钮:

public static void onBackPressed(final Activity activity) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle(R.string.on_back_button_title);
    builder.setMessage(R.string.on_back_button_message);
    builder.setPositiveButton(R.string.yes, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            activity.finish();
        }
    });
    builder.setNegativeButton(R.string.no, new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.show();
}
于 2012-11-05T07:19:55.253 回答
-1
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    /*if (keyCode == KeyEvent.KEYCODE_HOME) {
        Log.i("Home Button", "Clicked");
        // Toast.makeText(this,"Home Button Clicked",Toast.LENGTH_LONG).show();
        return false;
    }*/  // You cannot gain control over home button

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Toast.makeText(this, "Press back Button to pause Evaluation",
                Toast.LENGTH_LONG).show();
        Log.i("Back Button", "Clicked");
        return false;
        // finish();
    }
    return false;
}
于 2012-11-05T07:32:34.200 回答