我添加了 [viewbinding] 但我无法从 xml 文件中找到我的 [Button's id
I added [viewbinding] but I can't find my [Button's id from xml file
我只是无法从布局文件夹中获取 ID:
更多解释;
在 .kt 文件中的代码中,radio_group 是我的 ID,在下面的代码中全部为红色:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get radio group selected item using on checked change listener
radio_group.setOnCheckedChangeListener(
RadioGroup.OnCheckedChangeListener { group, checkedId ->
val radio: RadioButton = findViewById(checkedId)
Toast.makeText(applicationContext," On checked change :"+
" ${radio.text}",
Toast.LENGTH_SHORT).show()
})
// Get radio group selected status and text using button click event
button.setOnClickListener{
// Get the checked radio button id from radio group
var id: Int = radio_group.checkedRadioButtonId
if (id!=-1){ // If any radio button checked from radio group
// Get the instance of radio button using id
val radio:RadioButton = findViewById(id)
Toast.makeText(applicationContext,"On button click :" +
" ${radio.text}",
Toast.LENGTH_SHORT).show()
}else{
// If no radio button checked in this radio group
Toast.makeText(applicationContext,"On button click :" +
" nothing selected",
Toast.LENGTH_SHORT).show()
}
}
}
// Get the selected radio button text using radio button on click listener
fun radio_button_click(view: View){
// Get the clicked radio button instance
val radio: RadioButton = findViewById(radio_group.checkedRadioButtonId)
Toast.makeText(applicationContext,"On click : ${radio.text}",
Toast.LENGTH_SHORT).show()
}
}
radio_group 全部为红色错误,再次构建并按 ctrl+enter 后无法找到解决方案
有更好的方法请帮忙...
这不是您使用视图绑定的方式。您编写的代码是您使用 Kotlin 合成器执行此操作的方式,现在已弃用。启用 viewBinding 比简单地启用它需要更多的工作。我建议阅读此处有关如何操作的文档:
我只是无法从布局文件夹中获取 ID: 更多解释; 在 .kt 文件中的代码中,radio_group 是我的 ID,在下面的代码中全部为红色:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get radio group selected item using on checked change listener
radio_group.setOnCheckedChangeListener(
RadioGroup.OnCheckedChangeListener { group, checkedId ->
val radio: RadioButton = findViewById(checkedId)
Toast.makeText(applicationContext," On checked change :"+
" ${radio.text}",
Toast.LENGTH_SHORT).show()
})
// Get radio group selected status and text using button click event
button.setOnClickListener{
// Get the checked radio button id from radio group
var id: Int = radio_group.checkedRadioButtonId
if (id!=-1){ // If any radio button checked from radio group
// Get the instance of radio button using id
val radio:RadioButton = findViewById(id)
Toast.makeText(applicationContext,"On button click :" +
" ${radio.text}",
Toast.LENGTH_SHORT).show()
}else{
// If no radio button checked in this radio group
Toast.makeText(applicationContext,"On button click :" +
" nothing selected",
Toast.LENGTH_SHORT).show()
}
}
}
// Get the selected radio button text using radio button on click listener
fun radio_button_click(view: View){
// Get the clicked radio button instance
val radio: RadioButton = findViewById(radio_group.checkedRadioButtonId)
Toast.makeText(applicationContext,"On click : ${radio.text}",
Toast.LENGTH_SHORT).show()
}
} radio_group 全部为红色错误,再次构建并按 ctrl+enter 后无法找到解决方案 有更好的方法请帮忙...
这不是您使用视图绑定的方式。您编写的代码是您使用 Kotlin 合成器执行此操作的方式,现在已弃用。启用 viewBinding 比简单地启用它需要更多的工作。我建议阅读此处有关如何操作的文档: