无法从编辑文本中获取数据,Android Kotlin

can't get data from edit text, Android Kotlin

尝试从 edittext 获取数据时得到空数据 有我的代码

class LoginActivity : AppCompatActivity() {
lateinit var etUsername : EditText
lateinit var etPassword : EditText
var username = ""
var password = ""
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    etPassword = findViewById(R.id.tanggalLahir) as EditText
    etUsername = findViewById(R.id.nomerInduk) as EditText
    username = etUsername.text.toString()
    password = etPassword.text.toString()

    buttonLogin.setOnClickListener() {
        if (username.isEmpty()){
            Log.d("User", "Username = $username")
            nomerInduk.error = "Nomor Induk Kosong!"
            return@setOnClickListener
        }

        if (password.isEmpty()){
            Log.d("User", "Password = $password")
            tanggalLahir.error = "Tanggal Lahir Kosong!"
            return@setOnClickListener
        }

        login(username,password)
    }


}

还有我签入日志的时候 image check log

我已经将 edittext 更改为 textinputedittext 但还是一样

class LoginActivity : AppCompatActivity() {
lateinit var etUsername : EditText
lateinit var etPassword : EditText
var username = ""
var password = ""
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    etPassword = findViewById(R.id.tanggalLahir) as EditText
    etUsername = findViewById(R.id.nomerInduk) as EditText


    buttonLogin.setOnClickListener() {
username = etUsername.text.toString()
    password = etPassword.text.toString()
        if (username.isEmpty()){
            Log.d("User", "Username = $username")
            nomerInduk.error = "Nomor Induk Kosong!"
            return@setOnClickListener
        }

        if (password.isEmpty()){
            Log.d("User", "Password = $password")
            tanggalLahir.error = "Tanggal Lahir Kosong!"
            return@setOnClickListener
        }

        login(username,password)
    }


}
class LoginActivity : AppCompatActivity() {
lateinit var etUsername : EditText
lateinit var etPassword : EditText
var username = ""
var password = ""
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    etPassword = findViewById(R.id.tanggalLahir) as EditText
    etUsername = findViewById(R.id.nomerInduk) as EditText


    buttonLogin.setOnClickListener() {
    // once button click you have to get the current text value from the edit text what you have entered. 
    username = etUsername.text.toString()
    password = etPassword.text.toString()
        if (username.isEmpty()){
            Log.d("User", "Username = $username")
            nomerInduk.error = "Nomor Induk Kosong!"
            return@setOnClickListener
        }

        if (password.isEmpty()){
            Log.d("User", "Password = $password")
            tanggalLahir.error = "Tanggal Lahir Kosong!"
            return@setOnClickListener
        }

        login(username,password)
    }


}

你必须学习基本的 android 生命周期。在那里你第一次获得编辑文本值并分配给全局变量用户名和密码。但是在您输入一些并单击回车之后,您必须在单击提交按钮时获取新添加的文本值。这就是为什么那些分配代码进入按钮点击的原因。