Home > Blockchain >  Why text of EditText is not chanigng when i click the button
Why text of EditText is not chanigng when i click the button

Time:12-05

I have a app written in kotlin i want to fill the username field and password field and when i want to click the login button i want that the text of username field should be set to hello but this is not working

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var username = findViewById<EditText>(R.id.usernameText)
        var password = findViewById<EditText>(R.id.passwordText)
        var btn_Submit = findViewById<Button>(R.id.loginButton)

        btn_Submit.setOnClickListener()
        {
            val user = username.text
            val pass = password.text
            Toast.makeText(this@MainActivity, user, Toast.LENGTH_LONG).show()
            if(user.equals("amar"))
            {
                username.setText("hello")
            }
        }
    }
}

the username field which is placed in ui is of Plain Text and password field is Password I am very new to this please guide me where i went wrong

CodePudding user response:

Check this:

    btn_Submit.setOnClickListener()
    {
        val user:String = username.text.toString()
        val pass:String = password.text.toString()
        Toast.makeText(this@MainActivity, user, Toast.LENGTH_LONG).show()
        if(user.equals("amar"))
        {
            username.setText("hello")
        }
    }

or change the if statement like this:

        if(user=="amar"))
        {
            username.setText("hello")
        }
  • Related