Check if EditText is empty kotlin android

前端 未结 9 1843
悲哀的现实
悲哀的现实 2020-12-20 12:42

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

\"\"

package com.example.www.myappl         


        
相关标签:
9条回答
  • 2020-12-20 12:59

    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()
            }
        }
    
    0 讨论(0)
  • 2020-12-20 13:00

    Try this:

    if(TextUtils.isEmpty(editText.getText().toString())){    
        //Do
    }
    
    0 讨论(0)
  • 2020-12-20 13:12

    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....

    0 讨论(0)
提交回复
热议问题