我目前有一个工作if语句,但如果用户输入三个字段中的任何一个,他们可以发送数据我如何获得if else语句,所以除非填写所有三个字段,否则无法发送数据。
class MainActivity : AppCompatActivity() {
var editText: EditText? = null
var editText2: EditText? = null;
var editText3: EditText? = null;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editText = findViewById<EditText>(R.id.editText)
editText2 = findViewById<EditText>(R.id.editText2)
editText3 = findViewById<EditText>(R.id.editText3)
}
/** Called when the user taps the Send button */
fun sendMessage(view: View) {
if (editText!!.text.toString().length == 0) {
Toast.makeText(this, "You did not enter your name", Toast.LENGTH_SHORT).show();
if (editText2!!.text.toString().length == 0)
Toast.makeText(this, "You did not enter a email address", Toast.LENGTH_SHORT).show();
if (editText3!!.text.toString().length == 0)
Toast.makeText(this, "You did not enter a comapny name", Toast.LENGTH_SHORT).show();
return;
}else if {
val message1 = editText!!.text.toString()
val message2 = editText2!!.text.toString()
val message3 = editText3!!.text.toString()
val intent = Intent(this, DisplayMessageActivity::class.java).apply {
putExtra("EXTRA_MSG1", message1)
putExtra("EXTRA_MSG2", message2)
putExtra("EXTRA_MSG3", message3)
}
startActivity(intent)
}
}
}
答案 0 :(得分:3)
您可以使用when
声明。我已经创建了几个扩展函数来使代码更具可读性:
fun TextView?.getText(): String = this?.text?.toString() ?: ""
fun TextView?.isEmpty(): Boolean = this?.text?.isEmpty() ?: false
fun showToast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}
fun sendMessage(view: View) {
when {
editText.isEmpty() -> showToast("You did not enter your name")
editText2.isEmpty() -> showToast("You did not enter a email address")
editText3.isEmpty() -> showToast("You did not enter a comapny name")
else -> openActivity()
}
}
fun openActivity() {
val intent = Intent(this, DisplayMessageActivity::class.java).apply {
putExtra("EXTRA_MSG1", editText.getText())
putExtra("EXTRA_MSG2", editText2.getText())
putExtra("EXTRA_MSG3", editText3.getText())
}
startActivity(intent)
}
我还建议您将EditText
声明更改为lateinit var
,以免处理这些可空性检查:
lateinit var editText: EditText
lateinit var editText2: EditText
lateinit var editText3: EditText
答案 1 :(得分:0)
试试这段代码..
fun sendMessage(view:View) {
if (TextUtils.isEmpty(editText !!.text.toString())){
Toast.makeText(this, "You did not enter your name", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(editText2 !!.text.toString())){
Toast.makeText(this, "You did not enter a email address", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(editText3 !!.text.toString())){
Toast.makeText(this, "You did not enter a comapny name", Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(editText !!.text.toString())&&TextUtils.isEmpty(editText2 !!.
text.toString())&&TextUtils.isEmpty(editText3 !!.text.toString())){
val message1 = editText !!.text.toString()
val message2 = editText2 !!.text.toString()
val message3 = editText3 !!.text.toString()
val intent = Intent(this, DisplayMessageActivity:: class.java).apply {
putExtra("EXTRA_MSG1", message1)
putExtra("EXTRA_MSG2", message2)
putExtra("EXTRA_MSG3", message3)
}
startActivity(intent)
}
}
答案 2 :(得分:0)
您可以使用一个简短的函数来验证输入的数据,如
fun TextView.isEmpty():Boolean{
return this?.text?.isEmpty()
}
fun showMessage(msg:String){
//show some message here
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}
fun openActivity(m1:String="",m2:String="",m3:String=""){
val intent = Intent(this, DisplayMessageActivity:: class.java).apply {
putExtra("EXTRA_MSG1", m1)
putExtra("EXTRA_MSG2", m2)
putExtra("EXTRA_MSG3", m3)
}
startActivity(intent)
}
fun valid(){
if(editText.isEmpty() or editText1.isEmpty() or editText2.isEmpty()){
showMessage()
}else{
openActivity()
}
}
答案 3 :(得分:0)
我使用TextUtils.isEmpty()。根据文档,如果字符串为null或长度为零,则返回true。
https://developer.android.com/reference/android/text/TextUtils#isEmpty(java.lang.CharSequence)