android - 无法从警报对话框中的 EditText View 获取数据

标签 android xml kotlin android-edittext android-alertdialog

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

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

  • 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>
    

    最佳答案

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

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

    关于android - 无法从警报对话框中的 EditText View 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67136281/

    相关文章:

    java - 如何删除标记图标

    java - android market ://details? id=不适用于应用程序

    javascript - 如何在 HTML 中实现 XSLT 文件

    android - 如何在 Android 中使用 <layout> 标签

    intellij-idea - 如何让我的 JUnit 测试在我的 Kotlin+Gradle 项目中编译和运行?

    java - Jackson 与 Kotlin : how to serialize only annotated properties

    android - NullpointerException 覆盖 arrayadapter 中的 getview

    android - 通过 bundle 传递的 parcelable 对象是否与原始对象相同?

    java - 无法找到主类

    android - Kotlin - 伴生对象 fun 有什么作用?