我有一个带有AlertDiaolog
的Floating Action Button侦听器。我想使用XML中的按钮。如果我想为他们写一个onClickListener()
。
所以在Java中,我必须像这样初始化它:
butAdd = (Button)dialog.findViewById(R.id.btn_add)
butAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Some code
}
但是当我尝试使用时:
var butAdd = dialog?.findViewById(R.id.btn_add) as Button;
在 Kotlin 这是不正确的
那么有什么建议如何解决呢?听众有什么问题?
这是我的 float 操作按钮代码:
fab?.setOnClickListener {
diaolg = AlertDialog.Builder(this@Cards)
val linearlayout = getLayoutInflater().inflate(R.layout.add_password, null)
diaolg?.setView(linearlayout)
?.setTitle("Add a new password")
?.setCancelable(true)
var login = findViewById(R.id.login) as EditText
var password = findViewById(R.id.password) as EditText
var title = findViewById(R.id.title) as EditText
var butAdd = diaolg?.findViewById(R.id.btn_add) as Button
var butCancel = diaolg?.findViewById(R.id.btn_cancel) as Button
butAdd.setOnClickListener(View.OnClickListener {
fun onClick(v:View){
}
})
butCancel.setOnClickListener(View.OnClickListener {
fun onClick(v:View){
}
})
diaolg?.create()
diaolg?.show()
}
最佳答案
请使用找到ID
var butAdd = linearlayout.findViewById<Button>(R.id.btn_add) as Button;
关于android - 无法在Kotlin中初始化对话框中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41348671/