如果用户未填写字段'editTextNomeVelhinho',则我需要单击'b_avanca_c'按钮,并显示对话框警报,并且在用户填写'editText'后,必须转发布局'C_aviso',以及如果'从头开始填充editText”。在这种情况下,应用将始终为“ if”,即使在对话框中填充后也始终会出现。
fun b_avancar_c(view: View) {
val nomeIdoso :String
nomeIdoso=editTextNomeVelhinho.text.toString()
if(editTextNomeVelhinho.text.isEmpty()) {
buttonComecar.setOnClickListener {
val builder = AlertDialog.Builder(this@B_menu)
builder.setTitle("Atenção")
builder.setMessage("Deve colocar o seu nome para continuar")
builder.setPositiveButton("Continuar") { dialog, which ->
//Toast.makeText(applicationContext, "continuar", Toast.LENGTH_SHORT).show()
}
val dialog: AlertDialog = builder.create()
dialog.show()
}
}
else {
val it = Intent(this, C_aviso::class.java)
startActivity(it)
}
}
代码布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.example.asus.aprendecomigovelhinho.B_menu">
<Button
android:id="@+id/buttonComecar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@android:color/holo_green_dark"
android:text="Começar"
android:onClick="b_avancar_c"
android:textColor="@android:color/background_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.983"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.349" />
<EditText
android:id="@+id/editTextNomeVelhinho"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.921"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.026" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
单击按钮时会获得来自edittext的文本,因此每次都不会为空。
fun b_avancar_c(view: View) {
val nomeIdoso :String
nomeIdoso=editTextNomeVelhinho.text.toString()
if(editTextNomeVelhinho.text.isEmpty()) {
buttonComecar.setOnClickListener {
val builder = AlertDialog.Builder(this@B_menu)
builder.setTitle("Atenção")
builder.setMessage("Deve colocar o seu nome para continuar")
builder.setPositiveButton("Continuar") { dialog, which ->
//Toast.makeText(applicationContext,"continuar",Toast.LENGTH_SHORT).show()
}
val dialog: AlertDialog = builder.create()
dialog.show()
}
}
else {
val it = Intent(this, C_aviso::class.java)
startActivity(it)
}
}
答案 1 :(得分:0)
这里是一个简单的示例,说明如何使用https://developer.android.com/guide/topics/ui/dialogs文档中的Kotlin使用警报对话框
val builder: AlertDialog.Builder? = activity?.let {
AlertDialog.Builder(it)
}
builder!!.setMessage("Are you sure you want to log out?")
.setTitle("Log Out")
builder.apply {
setPositiveButton(R.string.user_log_out_dialog_no) { dialog, id ->
val selectedId = id
}
setNegativeButton(R.string.user_log_out_dialog_yes) { dialog, id ->
val selectedId = id
}
}
val dialog: AlertDialog? = builder.create()
dialog!!.show()
请记住,有些字符串是经过硬编码的。