I am new to Android app development. I am developing an app with two activities. Users can jump from MainActivity to Activity2 by entering some text and clicking a button. Fortunately, I have done that. But I was expecting if the user had not entered any text, it would show a toast "You have not entered anything" & definitely the Button click event will not happen in that case. What is the way to implement this feature using if-else?
Here's my MainActivity.kt
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
lateinit var nameInput : EditText
lateinit var sorryButton : Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nameInput = findViewById(R.id.nameInput)
sorryButton = findViewById(R.id.btn_sorry)
sorryButton.setOnClickListener {
val intent = Intent(this, MainActivity2::class.java)
val name = nameInput.editableText.toString()
intent.putExtra("name",name)
startActivity(intent)
}
}
}
If knows how to implement this please help.
Thanks.
Aucun commentaire:
Enregistrer un commentaire