如何取消警告对话框和后退按下的活动

时间:2012-06-22 07:33:05

标签: android

我的应用程序中有一个警告对话框用于身份验证。当我按下模拟器中的后退按钮时,警报对话框消失但活动仍在运行。现在我需要取消警告框以及后退的活动并想要转到移动主屏幕。我使用以下代码:

 @Override
        public void onStart() {
            super.onStart();        
            final SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            logutils.createlog();
            if (prefs.getString(getResources().getString(R.string.token), null) != null) {
                setContentView(R.layout.mainscreen);
                showButtons();
            } else {
                createDB();
                status = "completed";
                SharedPreferences.Editor prefsEditor = prefs.edit();
                prefsEditor.putString("status", status);
                prefsEditor.commit();
                final AlertDialog.Builder alert = new AlertDialog.Builder(this);
                LinearLayout login = new LinearLayout(this);
                TextView tvUserName = new TextView(this);
                TextView tvPassword = new TextView(this);
                TextView tvURL = new TextView(this);
                final EditText etUserName = new EditText(this);
                final EditText etPassword = new EditText(this);
                final EditText etURL = new EditText(this);
                if (dbhostname == null) {
                    etURL.setText(null);
                } else {
                    etURL.setText(dbhostname);
                }
                login.setOrientation(1); // 1 is for vertical orientation
                tvUserName.setText(getResources().getString(R.string.username));
                tvPassword.setText(getResources().getString(R.string.password));
                tvURL.setText("SiteURL");
                login.addView(tvURL);
                login.addView(etURL);
                login.addView(tvUserName);
                login.addView(etUserName);
                login.addView(tvPassword);
                etPassword.setInputType(InputType.TYPE_CLASS_TEXT
                        | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                login.addView(etPassword);
                alert.setView(login);
                alert.setTitle(getResources().getString(R.string.login));
                alert.setCancelable(true);
                alert.setPositiveButton(getResources().getString(R.string.login),
                        new DialogInterface.OnClickListener() {
                            public void onClick(final DialogInterface dialog,
                                    int whichButton) {                          
                                strhwdXml = etURL.getText().toString();
                                strUserName = etUserName.getText().toString();
                                XmlUtil.username = strUserName;
                                strPassword = etPassword.getText().toString();
                                if ((strUserName.length() == 0)
                                        && (strPassword.length() == 0)
                                        && (strhwdXml.length() == 0)) {
                                    Toast.makeText(
                                            getBaseContext(),
                                            getResources().getString(
                                                    R.string.userPassword),
                                            Toast.LENGTH_SHORT).show();
                                    onStart();
                                } else {
                                    new doprogress().execute();
                                    // doAuthentication();
                                }                           
                            }
                        });
                alert.setNegativeButton(getResources().getString(R.string.exit),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                dialog.cancel();
                                System.exit(0);
                            }
                        }).show();          
                alert.setOnCancelListener(new OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub
                        finish();
                        System.exit(0);
                    }              
                });
            }
        }

2 个答案:

答案 0 :(得分:2)

试试这段代码:

@Override

public void onBackPressed() {


AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

alertDialogBuilder.setTitle("Warning!");
alertDialogBuilder.setMessage("Do you want to Exit?");
alertDialogBuilder.setCancelable(false);

alertDialogBuilder.setPositiveButton("Ok",new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog,int id) {


        MainActivity.this.finish();
    }


  })

    .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {

        dialog.cancel();
    }
});

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();


}

答案 1 :(得分:1)

如果可以更改代码

new AlertDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)


new DialogInterface.OnCancelListener(){
                    @Override
                    public void onCancel(DialogInterface dialog) {

                        finish();

                    }
                }

AlertDialog.Builder

setOnKeyListener(DialogInterface.OnKeyListener onKeyListener)