Reputation:
I need to click to button 'b_avanca_c' if the user don't fill the field 'editTextNomeVelhinho' present the dialog alert and after the user fill the 'editText' must forward for layout 'C_aviso', as well as if 'editText' is filled from the beginning. In this case the aplications always goes to 'if' and even after it is filled in the dialog box always appears.
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)
}
}
Code layout
<?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>
Upvotes: 1
Views: 8844
Reputation: 822
Here is a simple example on how to use an Alert Dialog using Kotlin found in the documentation here https://developer.android.com/guide/topics/ui/dialogs
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()
Keep in mind that some strings are hardcoded.
Upvotes: 0
Reputation: 806
Text from edittext is got when clicking on button so it will not be empty everytime.
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)
}
}
Upvotes: 1