界面元素组成:

功能代码:
/*实现功能:
当输入内容后,欢迎文本发生相应改变,并清除掉文本域内容
当未输入任何内容时,弹出提示文本以警告用户*/
val greetingText = findViewById<TextView>(R.id.printer)
val inputField = findViewById<EditText>(R.id.inputer)
val submitButton = findViewById<Button>(R.id.btn)
submitButton.setOnClickListener {
val message = inputField.text.toString() //获取输入内容
if (message == "") { //当输入内容为空
//Toast为系统提示,第一个参数为Context(Activity亦为一种Context),
//第二个参数为提示文本,第三个参数为显示时长
Toast.makeText(
this,
"You haven't entered anything!",
Toast.LENGTH_SHORT
).show()
} else {
greetingText.text = "Welcomed! " + message
inputField.text.clear()
}
}