Check if EditText is empty kotlin android

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

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

\"\"

package com.example.www.myappl         


        
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    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()
            }
        }
    

提交回复
热议问题