检查EditText是否为空Kotlin android [英] Check if EditText is empty kotlin android

查看:316
本文介绍了检查EditText是否为空Kotlin android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查EditText是否为空?输入类型编号

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

推荐答案

以下是带有说明的完整示例.

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

这篇关于检查EditText是否为空Kotlin android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆