【发布时间】:2021-07-12 03:10:41
【问题描述】:
我创建了一个alertDialog,它基本上包含两个editTexts来输入数据(组件ID,组件名称)。当用户单击警报对话框中的“是”按钮时,新组件被添加到数据库中(截至目前我还没有在对话框中为“是”和“否”按钮添加了任何点击监听器),但由于某种原因,我无法从编辑文本中获取数据。我是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>
【问题讨论】:
标签: android xml kotlin android-edittext android-alertdialog