Android Dialog, keep dialog open when button is pressed

后端 未结 7 1313
故里飘歌
故里飘歌 2020-11-30 01:59

I would like to keep my dialog open when I press a button. At the moment it\'s closing.

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder         


        
相关标签:
7条回答
  • 2020-11-30 02:55

    This is how I manage to create a persistent popup when changing password.

    // Login Activity
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.SetIcon(Resource.Drawable.padlock);
    alert.SetCancelable(false);
    
    var changepass = LayoutInflater.From(this);
    var changePassLayout = changepass.Inflate(Resource.Layout.ChangePasswordLayout, null);
    
    alert.SetView(changePassLayout);
    
    txtChangePassword = (EditText)changePassLayout.FindViewById(Resource.Id.txtChangePassword);
    txtChangeRetypePassword = (EditText)changePassLayout.FindViewById(Resource.Id.txtChangeRetypePassword);
    
    alert.SetPositiveButton("Change", delegate {
        // You can leave this blank because you override the OnClick event using your custom listener
    });
    
    alert.SetNegativeButton("Cancel", delegate {
        Toast.MakeText(this, "Change password aborted!", ToastLength.Short).Show();
    });
    
    AlertDialog changePassDialog = alert.Create();
    changePassDialog.Show();
    
    // Override OnClick of Positive Button
    Button btnPositive = changePassDialog.GetButton((int)Android.Content.DialogButtonType.Positive);
    btnPositive.SetOnClickListener(new CustomListener(changePassDialog, empDetailsToValidate.EmployeeID));
    

    // My Custom Class
    class CustomListener : Java.Lang.Object, View.IOnClickListener, IDialogInterfaceOnDismissListener
    {
        AlertDialog _dialog;
        EditText txtChangePassword;
        EditText txtChangeRetypePassword;
    
        EmployeeDetails _empDetails;
        string _workingEmployeeID;
    
        public CustomListener(AlertDialog dialog, string employeeID)
        {
            this._dialog = dialog;
            this._workingEmployeeID = employeeID;
        }
        public void OnClick (View v)
        {
            _empDetails = new EmployeeDetails(v.Context);
    
            txtChangePassword = (EditText)_dialog.FindViewById (Resource.Id.txtChangePassword);
            txtChangeRetypePassword = (EditText)_dialog.FindViewById (Resource.Id.txtChangeRetypePassword);
    
            if (!(txtChangePassword.Text.Equals(txtChangeRetypePassword.Text))) {
                Show ();
                Toast.MakeText(v.Context, "Password not match.", ToastLength.Short).Show();
            } else if (txtChangePassword.Text.Trim().Length < 6) {
                Show ();
                Toast.MakeText(v.Context, "Minimum password length is 6 characters.", ToastLength.Short).Show();
            } else if ((txtChangePassword.Text.Equals(LoginActivity.defaultPassword)) || (txtChangePassword.Text == "" || txtChangeRetypePassword.Text == "")) {
                Show ();
                Toast.MakeText(v.Context, "Invalid password. Please use other password.", ToastLength.Short).Show();
            } else {
                int rowAffected = _empDetails.UpdatePassword(_workingEmployeeID, SensoryDB.PassCrypto(txtChangePassword.Text, true));
                if (rowAffected > 0) {
                    Toast.MakeText(v.Context, "Password successfully changed!", ToastLength.Short).Show();
                    _dialog.Dismiss();
                } else {
                    Toast.MakeText(v.Context, "Cant update password!", ToastLength.Short).Show();
                    Show();
                }
            }
        }
        public void OnDismiss (IDialogInterface dialog)
        {
            if (!(txtChangePassword.Text.Equals (txtChangePassword.Text))) {
                Show ();
            } else {
                _dialog.Dismiss();
            }
        }
        public void Show ()
        {
            _dialog.Show ();
        }
    }
    

    BTW, i use Mono for Android not Eclipse.

    0 讨论(0)
提交回复
热议问题