Can somebody tell me what I doing wrong? I want to make a simple login activity. Insert password, for example "112", activate by ENTER on keyboard and get to new activity. I do it this way but it doesn't work
val password: EditText = findViewById(R.id.password)
password.setOnKeyListener{ _, keyCode, keyEvent ->
if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
if (password.equals("112")) {
Intent(this, MainActivity2::class.java).also { startActivity(it) }
}
return@setOnKeyListener true
}
false
}
CodePudding user response:
You are comparing editText instance with 112 which is causing the trouble. You need to compare editText text with the actual text i.e 112
val password: EditText = findViewById(R.id.password)
password.setOnKeyListener{ _, keyCode, keyEvent ->
if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
if (password.getText().toString().equals("112")) { // use getText() to get the text of password editText here
Intent(this, MainActivity2::class.java).also { startActivity(it) }
}
return@setOnKeyListener true
}
false
}