【问题标题】:Unable to get the data from EditText view in the alert dialog无法从警报对话框中的 EditText 视图中获取数据
【发布时间】:2021-07-12 03:10:41
【问题描述】:

我创建了一个alertDialog,它基本上包含两个editTexts来输入数据(组件ID,组件名称)。当用户单击警报对话框中的“是”按钮时,新组件被添加到数据库中(截至目前我还没有在对话框中为“是”和“否”按钮添加了任何点击监听器),但由于某种原因,我无法从编辑文本中获取数据。我是android应用程序开发的新手,请帮帮我.....在此先感谢:)

注意: 1. 我正在使用 viewModel.flag,以便 alertDialog 保持持续旋转。

  1. onCancel() 方法只是取消了alertDialog(我打算用它来更新某些参数)。

这是创建警报对话框的 kotlin 代码。

fun onAddComponent()
    {
        val builder = AlertDialog.Builder(requireActivity())

        builder?.setTitle("New Component Info")
        val inflater = requireActivity().layoutInflater

        builder?.setView(inflater.inflate(R.layout.new_comp, null))

        builder?.setPositiveButton(R.string.Okay_button){ dialogInterface, which->
            viewModel.flag = false
            val editText : EditText? =  compAlert?.findViewById(R.id.new_Component)
            println(editText.toString())
        }
        builder?.setNegativeButton(R.string.NO){ dialogInterface, which->
            viewModel.flag = false
        }



            compAlert= builder?.create()
        compAlert?.setOnCancelListener{OnCancel()}
        viewModel.flag = true
        compAlert?.show()

    }

这是我在 onAddComponent() 中膨胀的 xml 布局

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <EditText
        android:id="@+id/new_Component"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="comp Id"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/new_component_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="comp name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="@+id/new_Component"
        app:layout_constraintStart_toStartOf="@+id/new_Component"
        app:layout_constraintTop_toBottomOf="@+id/new_Component" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android xml kotlin android-edittext android-alertdialog


    【解决方案1】:

    要获取 EditText 中的文本,您需要执行代码中的 editText.getText().toString()NOT editText.toString()

    在您的代码中,editText.toString() 将返回 editText 对象的字符串表示形式。参见String的toString()方法here的文档。

    【讨论】:

    • 是的...我刚刚这样做了..我在发布问题后意识到这一点并且应该更改 xml 代码中的输入类型..它工作正常..非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 2016-06-16
    • 2013-04-22
    • 2012-08-06
    • 2013-09-23
    • 2021-01-06
    • 1970-01-01
    相关资源
    最近更新 更多