Check if EditText is empty kotlin android

2019-02-24 07:32发布

问题:

How do you check if an EditText is empty? input type number

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()
    }
}

回答1:

Here is the full example with explanation.

    //init the edittext
    val etMessage = findViewById(R.id.et_message) as EditText
    //init the button
    val btnClick = findViewById(R.id.btn_click) as Button

    btnClick.setOnClickListener{
        //read value from EditText to a String variable
        val msg: String = etMessage.text.toString()

        //check if the EditText have values or not
        if(msg.trim().length>0) {
            Toast.makeText(applicationContext, "Message : "+msg, Toast.LENGTH_SHORT).show()
        }else{
            Toast.makeText(applicationContext, "Please enter some message! ", Toast.LENGTH_SHORT).show()
        }
    }


回答2:

Harness Kotlin power by using inline extension functions:

editText.text.isNotEmpty().apply { 
    //do something
 }

or use let



回答3:

Hey I am using like this in kotlin

 val input = editText?.text.toString().trim()
    if (input.isNullOrBlank()) {
       //Your code for blank edittext
    }

Hope this will help you..let me know if any issue....



回答4:

You can be done by below way

if (mEdtDeviceName.text.toString().trim().isNotEmpty() || 
    mEdtDeviceName.text.toString().trim().isNotBlank()) {
       // your code
} else {
    Toast.makeText(activity, "Error Msg", Toast.LENGTH_SHORT).show()
}


回答5:

 var userName = editText!!.text.toString().trim()
 if (userName.equals("")) {
     //TODO Something Here
 }


回答6:

Try this:

if(TextUtils.isEmpty(editText.getText().toString())){    
    //Do
}


回答7:

Been a new guy Tried lots and this Worked for me

     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:

try this out:

//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:

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)
            }

        }


    }


}