开发者

make an action from the back button of the device

开发者 https://www.devze.com 2023-03-04 19:39 出处:网络
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

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消