在AlertDialog上使用CustomLayout

问题描述 投票:0回答:1

我想为我的应用程序使用一个简单的AlertDialog,因为我需要它在AlertDialog上使用两个或多个EditText。这就是为什么我在AlertDialog上使用自定义布局的原因。一切都已经设置好了,但是我无法从EditText字段中获取数据。可能我错过了一些确实很简单的事情,但这可能是一个更大的问题,这就是为什么我要问这个问题。这是代码块。

 val alert = AlertDialog.Builder(activity)
    val layout = R.layout.alert_view
    val customLayout: View = layoutInflater.inflate(layout, null)
    alert.setView(customLayout)
    var etCourse1 = view?.findViewById<EditText>(R.id.etCourse1)
    var etCourse2 = view?.findViewById<EditText>(R.id.etCourse2)
    alert.setIcon(R.drawable.ic_warning_black_24dp)

    alert.setPositiveButton("Confirm")
    {
            dialog, _ ->
        tempEditTextValue = etCourse1?.text.toString() //editText.text.toString()
        tempEditTextValue2 = etCourse2?.text.toString()
        if(tempEditTextValue.isEmpty())
        {
            Toast.makeText(activity,"TextField is Empty", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }
        else
        {

            //FunctionHere
        }

    }
    alert.setNegativeButton("Cancel")
    {
            dialog, _ ->
        dialog.dismiss()
    }
    alert.setCancelable(false)


    alert.show()

而且,我可以使用自定义按钮作为“确认”和“取消”按钮吗?谢谢!

android kotlin android-alertdialog
1个回答
0
投票

我的视图是customLayout,但是我正在尝试使用视图来获取该EditText。

EditText的必须像;

    var etCourse1 = customLayout?.findViewById<EditText>(R.id.etCourse1)
    var etCourse2 = customLayout?.findViewById<EditText>(R.id.etCourse2)
© www.soinside.com 2019 - 2024. All rights reserved.