-1

我想使用手机本身的后退按钮来显示一个对话框,询问我是否确定要退出?

 public void onBackPressed() {
    // TODO Auto-generated method stub

    AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
    dialog.setTitle("Confirm");
    dialog.setMessage("Are you sure you want to exit?");
    return;
 }



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText text=(EditText)findViewById(R.id.Text);
    TextWatcher watch=new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            onBackPressed();
        }
    };
    text.addTextChangedListener(watch);
}
4

4 回答 4

2

正如我认为你onBackPressed()是处理android后退键的覆盖方法。

只需添加dialog.create().show()到您的onBackPressed();.

它将显示AlertDialog ..

onBackPressed();afterTextChanged(Editable s)else 中删除,每当用户尝试在EditText..中输入某些字符时,它都会调用它。

更新:

onBackPressed()你应该像的另一件事,

@Override
public void onBackPressed()
{
 super.onBackPressed();

 AlertDialog.Builder dialog = new AlertDialog.Builder(TestActivity.this);
 dialog.setTitle("Confirm");
 dialog.setMessage("Are you sure you want to exit?");
 dialog.create().show();
}

不改就改。

于 2012-08-27T13:25:11.087 回答
0

将此代码用于后退按钮事件

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_BACK) {
     onBackPressed();
     return true;
   }
 return super.onKeyDown(keyCode, event);
}

这是警报对话框

private void onBackPressed() {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    alertDialogBuilder.setTitle("Do you really want to exit?");
    alertDialogBuilder
            .setMessage("Click yes to exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    ThisActivity.this.finish();
                }
              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });             
            AlertDialog alertDialog = alertDialogBuilder.create(); 
            alertDialog.show();
}
于 2012-08-27T13:25:19.523 回答
0

尝试这个。

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
    {
        showDialogForCloseApplication();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

private void showDialogForCloseApplication()
    {
        try
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(getResources().getString(R.string.name));
            builder.setMessage("Are you sure you want to exit?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
               {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                       finish();
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() 
               {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                        dialog.cancel();
                   }
               });
            AlertDialog alert = builder.create();
            alert.show();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
于 2012-08-27T13:25:21.023 回答
0

*第一种方式:onKeyDown *尝试在当前活动中使用 @Override方法:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if (keyCode == event.KEYCODE_BACK) {
        showAlert("", "Do you really want to exit?");
    }
    return super.onKeyDown(keyCode, event);
}

private void showAlert(final String title, final String msg) {
    AlertDialog.Builder dialouge = new AlertDialog.Builder(CategoriesList.this);
    dialouge.setMessage(msg);
    dialouge.setPositiveButton(" Yes ", new DialogInterface.OnClickListener() {
        public void onClick( DialogInterface arg0, int arg1) {
            CurrentActivity.this.finish();
        }
    });
    dialouge.setNegativeButton(" No ", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
        }
    });
    dialouge.show();
}

第二种方式: @Override onBackPresed()

public void onBackPresed(){

   Log.d(TAG,"inside onBackPressed()");
       showAlert("", "Do you really want to exit?");
}
于 2012-08-27T13:26:31.350 回答