在计算数字时检查EditText是否为空

时间:2017-06-10 14:38:23

标签: android kotlin

我正在尝试构建一个年龄计算器应用程序。当我没有在其中一个EditTexts中输入数字并点击计算Button时,我的应用就崩溃了! "App has stopped" and click the photo

我尝试设置yearbirth.isEmpty() || yearbirth.equals("") || yearbirth == null,但没有任何效果。

代码: -

class MainActivity : AppCompatActivity() {
var yearage:Int?= null
var monthage:Int?= null
var dayage:Int?= null

@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    buGetAge.setOnClickListener({
        val year= Calendar.getInstance().get(Calendar.YEAR)
        val month= Calendar.getInstance().get(Calendar.MONTH)
        val day= Calendar.getInstance().get(Calendar.DAY_OF_MONTH)

        var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1
        val monthage:Int?
        val dayage:Int?

        if (yearbirth== null) {
            Toast.makeText(this, "You have to enter your year berth", Toast.LENGTH_LONG).show()
        } else {
            if (month < Integer.parseInt(monthbirth.text.toString())){
                monthage=(Integer.parseInt(monthbirth.text.toString())- month- 12)* -1
            }else if (month > Integer.parseInt(monthbirth.text.toString())) {
                yearage=year- Integer.parseInt(yearbirth.text.toString())
                monthage= (Integer.parseInt(monthbirth.text.toString())- month)* -1
            }else {
                yearage=year- Integer.parseInt(yearbirth.text.toString())
                monthage=0
            }
            if (day < Integer.parseInt(daybirth.text.toString())){
                dayage= (Integer.parseInt(daybirth.text.toString())- day- 30)* -1
            }else if (day > Integer.parseInt(daybirth.text.toString())){
                dayage= day- Integer.parseInt(daybirth.text.toString())
            }else {
                dayage=0
            }

            val a= (yearage* 12)+ monthage
            val b= (a * 30)+ dayage

            val ageinyears= "$yearage years"
            val ageinmonths= "$a months"
            val ageindays= "$b days"

            txtshow.text = "Your age is $yearage years, $monthage months and $dayage days"
            txtshow2.text = "\nYOUR AGE:\nin years:\n\nin months:\n\nin days:"
            txtshow3.text = "\n\n$ageinyears \n\n$ageinmonths \n\n $ageindays "

            this.yearage=yearage
            this.monthage=monthage
            this.dayage=dayage
        }
    })
}
}

2 个答案:

答案 0 :(得分:0)

您可以像这样检查空Input

newString = Integer.toString(yearbirth).trim();
if (newString.matches("")) {
    Toast.makeText(this, "EditText is empty", Toast.LENGTH_SHORT).show();
    return;
}

答案 1 :(得分:0)

在尝试将yearbirth.text.toString()解析为整数之前验证var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1 是否为空(以及它是否为有效整数。)

要实现这一目标,请将您显示的行放在代码中:

if

if(yearbirth.text.toString().matches(Regex("\\d+"))) var yearage = year - Integer.parseInt(yearbirth.text.toString()) -1 内验证输入是否有效,如下所示:

"state_machines[?starts_with(name,'hello')].state_machine_arn"