i would like to have an action when the user press the back button from his device.how i can make it开发者_C百科?thanks
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
if(canPressBack) {
return super.onKeyDown(keyCode, event);
}
}
return true;
}
Activity has public void onBackPressed()
callback method for it.. use it
Try this code
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
String dialog_message = "Please Select at least one item or press Cancel button";
if(keyCode==KeyEvent.KEYCODE_BACK)
{
AlertDialog.Builder invalid_input_dialog = new AlertDialog.Builder(Classname.this);
invalid_input_dialog.setTitle("Application name")
.setMessage(dialog_message)
.setCancelable(true)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
精彩评论