我想在用户按下手机时显示警告框。 所以我只使用::
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
try {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("This is the alertbox!");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
}
});
alertbox.show();
stopService(new Intent(this, BackServices.class));
finish();
} catch (Exception e) {
// TODO: handle exception
}
}
return super.onKeyDown(keyCode, event);
}
but problem is
:当我按下alertbox show但它退出应用程序时。我希望停止退出应用程序,直到用户按下yes按钮。如果用户按否,则返回应用程序
答案 0 :(得分:2)
有两件事可以实现
<强> 1)。强>
stopService(new Intent(this, BackServices.class));
finish();
从此处删除finish();
,因为这是您的活动关闭的原因
2。)而不是
return super.onKeyDown(keyCode, event);
你可以像这样返回假,
return false;
答案 1 :(得分:0)
我的方法就是这样,
public void onBackPressed() {
//finish();
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setTitle("Warning");
alertbox.setMessage("Exit Application?");
alertbox.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
});
alertbox.setNegativeButton("No", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertbox.show();
}
或者考虑一下你自己的代码,我可能会做这样的事情。
你必须在像这样的正面按钮点击事件中提供finish(),
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
try {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("This is the alertbox!");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
stopService(new Intent(this, BackServices.class));
finish();
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
}
});
alertbox.show();
} catch (Exception e) {
// TODO: handle exception
}
}
return super.onKeyDown(keyCode, event);
}
答案 2 :(得分:0)
Keep your code as it is...with adding finish() call in positive button click
And override onBackPressed() method with doing nothin
public void onBackPressed() {
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
try {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("This is the alertbox!");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
stopService(new Intent(this, BackServices.class));
finish();
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
}
});
alertbox.show();
} catch (Exception e) {
// TODO: handle exception
}
}
return super.onKeyDown(keyCode, event);
}
答案 3 :(得分:0)
更好的解决方案是返回true
以表明您已经消耗了该事件。
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// do your stuff
....
....
return true; // control will return from here. super will not be called.
}
return super.onKeyDown(keyCode, event);
}