代码之家  ›  专栏  ›  技术社区  ›  HelloCW

在Kotlin中使用AlertDialog时,如何在输入错误密码时返回到输入用户界面?

  •  1
  • HelloCW  · 技术社区  · 7 年前

    我希望在启动应用程序时打开密码输入对话框。

    如果输入正确的密码,对话框将关闭并显示主用户界面。

    如果我输入错误密码,对话框将保持打开状态,并需要用户再次输入。

    我该怎么做?谢谢!

    目前,无论输入正确或错误的密码,对话框总是关闭的。

      override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.layout_main)      
            showInputPasswordDialog()
        }  
    
    
        private fun showInputPasswordDialog {    
            val editText = EditText(this)
            val inputDialog = AlertDialog.Builder(this)
            inputDialog.setTitle("Input")
                       .setView(editText)
                       .setCancelable(false)
                       .setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, which ->
                               finish();
                       })
    
            inputDialog.setPositiveButton("OK",
                    DialogInterface.OnClickListener { dialog, which ->
                        val password= editText.text.toString()
                        if (password=="aa"){
                            //close the dialog
                        }else{
                            toast("Password error")
                            //Return for input again
                        }
                    }).show()
        }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   rafa    7 年前

    如果在alertDialog.onshowlistener中处理onclick listener for positive按钮,则可以避免对话框关闭流。请在下面找到修改的代码。

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.layout_main)      
            showInputPasswordDialog()
        }  
    
    
        private fun showInputPasswordDialog {    
            val editText = EditText(this)
            val inputDialog = AlertDialog.Builder(this)
            inputDialog.setTitle("Input")
                       .setView(editText)
                       .setCancelable(false)
                       .setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, which ->
                               finish();
                       })
    
            inputDialog.setPositiveButton("OK",null)
    
           // modified code starts here
            val mAlertDialog = inputDialog.create()
    
            mAlertDialog.setOnShowListener(DialogInterface.OnShowListener {
            val b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
            b.setOnClickListener(View.OnClickListener {
    
              val password= editText.text.toString()
                    if (password=="aa"){
                        mAlertDialog.dismiss();
                        //close the dialog
                    }else{
                        toast("Password error")
                        //Return for input again
                    }
    
            })
        })
    
        mAlertDialog.show() 
    
        }
    

    裁判: How to prevent a dialog from closing when a button is clicked