(kotlin) editText.toString().toInt() 在 android studio 中不起作用

(kotlin) editText.toString().toInt() isn't work in anroid studio

val editText1 = findViewById<EditText>(R.id.editText1);

if(comNum != editText1.toString().toInt() ){

 View4.text = "오답"
            } else View4.text = "정답"

安装的 apk 不工作。我认为 edittext.toString.toInt 是错误的。

EditText 中得到 StringeditText1.getText().toString().toInt()

错误

 editText1.toString().toInt()

应该是

editText1.text.toString().toInt()

仅供参考

toInt() Parses the string as an Int number and returns the result. if the string is not a valid representation of a number you will receive NumberFormatException .

试试这个

 val editText1 = findViewById<EditText>(R.id.editText1);

  if(comNum != Integer.parseInt(editText1.text.toString()) ){

     View4.text = "오답"
        } else View4.text = "정답"

试试下面的代码,你不能使用 editText1.toString().toInt()

  var value: Int

  try {
         value =  editText1.text.toString().toInt();
  } catch (e: NumberFormatException) {
         // value of editText1 is a invalid Integer
  }
  if(comNum != value ){
  View4.text = "오답"
  } else View4.text = "정답"

希望这对您有所帮助

使用

editText1.text.toString().toInt()