SignupActivity.kt
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.EditText
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_signup.*
class SignupActivity:AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_signup)
val signname =findViewById<EditText>(R.id.signNameTxt)
val s=signname!!.text.toString()
fun validate(){
if(s.isNotEmpty()){
Toast.makeText(this,"Working :)",Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(this,"Not Working :(",Toast.LENGTH_SHORT).show()
}
}
signNxtBtn.setOnClickListener{
validate()
}
}
public override fun onDestroy() {
super.onDestroy()
}
}
This is a simple code for signup activity to validate whether the field is empty or not for further processing
if(s.isNotEmpty()){
Toast.makeText(this,"Working :)",Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(this,"Not Working :(",Toast.LENGTH_SHORT).show()
}
In this part of the code, whether I enter any value inside the text field, the application is returning the else part.
You're reading the edittext value too early. Move the
val s=signname!!.text.toString()
inside your validate()
function.
**
Use your code like this.
**
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val s = tvDisplay.text.toString()
btnOk.setOnClickListener {
validate(s)
}
}
private fun validate(s:String) {
if(s.isNotEmpty()){
Toast.makeText(this,"Working :)",Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(this,"Not Working :(",Toast.LENGTH_SHORT).show()
}
} }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.