如何检查EditText是否为空?输入类型编号
package com.example.www.myapplication
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val inter:Int=editText.text.toString().toInt()
val year: Int = Calendar.getInstance().get(Calendar.YEAR)
val res:Int=year-inter
textView.text=res.toString()
}
}
答案 0 :(得分:6)
使用内联扩展功能来利用Kotlin电源:
editText.text.isNotEmpty().apply {
//do something
}
或使用let
答案 1 :(得分:5)
以下是完整的解释说明。
ui-view
答案 2 :(得分:2)
嘿,我在kotlin中这样使用
val input = editText?.text.toString().trim()
if (input.isNullOrBlank()) {
//Your code for blank edittext
}
希望这会帮助你..让我知道是否有任何问题......
答案 3 :(得分:2)
你可以通过以下方式完成
if (mEdtDeviceName.text.toString().trim().isNotEmpty() ||
mEdtDeviceName.text.toString().trim().isNotBlank()) {
// your code
} else {
Toast.makeText(activity, "Error Msg", Toast.LENGTH_SHORT).show()
}
答案 4 :(得分:1)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnSignUp : Button = findViewById(R.id.signUp)
val et_username : EditText = findViewById(R.id.etUsername)
val et_email : EditText = findViewById(R.id.etEmail)
val et_password : EditText = findViewById(R.id.etPassword)
btnSignUp.setOnClickListener{
val user_msg_error: String = et_username.text.toString()
//check if the EditText have values or not
if(user_msg_error.trim().isEmpty()) {
et_username.error = "Required"
Toast.makeText(applicationContext, "User Name Required ", Toast.LENGTH_SHORT).show()
}
else if (et_email.text.toString().trim().isEmpty()) {
et_email.error = "Required"
Toast.makeText(applicationContext, "Email Required ", Toast.LENGTH_SHORT).show()
}
else if (et_password.text.toString().trim().isEmpty()) {
et_password.error = "Required"
Toast.makeText(applicationContext, "Password Required ", Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(applicationContext, "Login Successful ", Toast.LENGTH_SHORT).show()
// After successful login u will move on next page/ activity
val i = Intent(this,SecondActivity::class.java)
startActivity(i)
}
}
}
}
答案 5 :(得分:0)
试试这个:
if(TextUtils.isEmpty(editText.getText().toString())){
//Do
}
答案 6 :(得分:0)
var userName = editText!!.text.toString().trim()
if (userName.equals("")) {
//TODO Something Here
}
答案 7 :(得分:0)
是一个新人尝试了很多,这为我工作
if(!editTextTerminalName.text.toString().trim().isNotEmpty()) {
editTextTerminalName?.error = "Required"
}else if(!editTextPassword.text.toString().trim().isNotEmpty()){
editTextPassword?.error = "Required"
}else{
avi.visibility= View.VISIBLE // v letter should be capita
}
答案 8 :(得分:0)
试试这个:
//button from xml
botton.setOnClickListener{
val new=addText.text.toString()//addText is an EditText
if(new=isNotEmpty())
{
//do something
}
else{
Toast.makeText(applicationContext, "Enter some message ", Toast.LENGTH_SHORT).show()
}
}
答案 9 :(得分:0)
相同的解决方案,但使用类 TextUtil 和 .isEmpty(charsequence:)
btnGo.setOnClickListener{
val input1 = etName.text.toString.trim() // 1
if(TextUtils.isEmpty(input1)){ // 2
etName.error = "Enter a name" // 3
return@setOnClickListener //4
}
//code to store a Bundle or insert in a sqlitedb etc
// go to secondactiviy
}
答案 10 :(得分:-1)
if (regemail.isEmpty())
{
Toast.makeText(this,"Enter Email..!!!",Toast.LENGTH_LONG).show()
}