满足条件时使肯定按钮无效

时间:2018-10-24 18:37:52

标签: android kotlin

在我的应用程序中,我有一个带有EditText字段的弹出窗口。如果有多个点我想使肯定按钮失效,则意味着使其变为灰色且无法单击。我查看了Java代码,并尝试了几种解决方案,但是我不知道如何使它工作。 我的代码:

 fun dialogBuilder(){
        var dots = 0
        lateinit var inputText: String
        val builder = AlertDialog.Builder(this)
        builder.setTitle(getString(R.string.moneyStatus))
        val input = EditText(this)
        input.inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_SIGNED
        input.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
        input.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable) {}

            override fun beforeTextChanged(s: CharSequence, start: Int,
                                           count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int,
                                       before: Int, count: Int) {

                val length = s.length
                if (length > 0){
                    for (i in 0..length){
                        if (i < length) {
                            if (s[i] == '.') {
                                dots += 1
                            }
                        }
                    }
            }
            }
        })
        builder.setView(input)
        builder.setPositiveButton("OK") {
            dialog, which -> inputText = input.text.toString()
            money.setText(inputText)
        }
        builder.setNegativeButton(getString(R.string.cancel)) {
            dialog, which -> dialog.cancel()
        }
        if (dots > 1){
            //gray out "OK" button
        }
        builder.show()
    }
}

2 个答案:

答案 0 :(得分:1)

在谷歌搜索时,我对这个问题question之以鼻,应该可以回答您。

How to disable / enable dialog negative positive buttons?

我将代码编译为kotlin,并且似乎可以正常工作,或者至少是可编译的:D

fun dialogStuff() {
    val builder = AlertDialog.Builder(this)
    builder.setIcon(android.R.drawable.ic_dialog_info)
    builder.setTitle("Alert dialog title")
    builder.setMessage("This is the example code snippet to disable button if edittext attached to dialog is empty.")
    builder.setPositiveButton("PositiveButton"
    ) { arg0, arg1 ->
        // DO TASK
    }
    builder.setNegativeButton("NegativeButton"
    ) { arg0, arg1 ->
        // DO TASK
    }
    val input = EditText(this)
    val lp = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT)
    input.layoutParams = lp
    builder.setView(input)
    val dialog = builder.create()
    dialog.show()
    (dialog as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false
    input.addTextChangedListener(object : TextWatcher {
        override fun onTextChanged(s: CharSequence, start: Int, before: Int,
                                   count: Int) {
        }

        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int,
                                       after: Int) {
        }

        override fun afterTextChanged(s: Editable) {
            // Check if edittext is empty
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = !TextUtils.isEmpty(s)

        }
    })
}

答案 1 :(得分:0)

要禁用按钮:

 mButtonToggle.setEnabled( false );

启用按钮:

 mButtonToggle.setEnabled( true );