我的if语句在Kotlin android中永远不会发生

时间:2019-03-11 00:56:28

标签: android kotlin

如果停顿从未发生过。所以请帮我我尝试更改一些东西,希望它能工作,但没成功,只有站台工作。

package com.example.managemntsystem

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        var input = editText.text
        var empList = arrayListOf<String>("jacob", "raf", "boss", "john")


        button.setOnClickListener {

            if (input == emplist) {

                textView2.setText("WORKING")

            }else{

                textView2.setText("Not working")

            }
        }


    }
}

3 个答案:

答案 0 :(得分:3)

您还可以使用in运算符:

if (input in empList)

答案 1 :(得分:0)

if(empList.contains(input))

我认为这可以解决您的问题

答案 2 :(得分:0)

原因是您的var input = editText.text再也不会设置(即使它是var),因此在这种情况下,它与val没什么不同。您要么需要重新分配它,要么直接使用.text

  1. 再次设置:

var input = editText.text var empList = arrayListOf(“ jacob”,“ raf”,“ boss”,“ john”)

button.setOnClickListener {

input = editText.text // add this line

f (input == emplist) {

        textView2.setText("WORKING")

    }else{

        textView2.setText("Not working")

    }
}
  1. 或者根本不使用输入法,而只使用文本本身:

    button.setOnClickListener {
    
    if (editText.text == emplist) {
    
        textView2.setText("WORKING")
    
    }else{
    
        textView2.setText("Not working")
    
            }
    }
    
相关问题