在android中按下单选按钮时图像不会改变
我正在尝试在radiogroup
中在三个radiobutton
之间更改图像,以便每个按钮在相同的ImageView
上显示不同的图像。但这无法正常工作,所以显然我缺少一些东西。
该按钮在XML
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="39dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline3"
app:layout_constraintStart_toEndOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
按钮组的kotlin代码为:
when (binding.radioGroup.checkedRadioButtonId) {
binding.radioButtonLondon.id -> {
binding.textClock.timeZone = "Europe/London"
binding.imageView.setImageResource(R.drawable.london)
}
binding.radioButtonNewYork.id -> {
binding.textClock.timeZone = "America/New_York"
binding.imageView.setImageResource(R.drawable.newyork)
}
binding.radioButtonBaijing.id -> {
binding.textClock.timeZone = "CST6CDT"
binding.imageView.setImageResource(R.drawable.baijing)
}
}
但这不起作用,图像不会更改。那我想念什么?
I'm trying to change an image between three RadioButton
in a RadioGroup
so that each button displays a different image on the same ImageView
. But it's not working, so clearly I'm missing something.
The button is declared in XML
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="39dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline3"
app:layout_constraintStart_toEndOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
the kotlin code for the button group is:
when (binding.radioGroup.checkedRadioButtonId) {
binding.radioButtonLondon.id -> {
binding.textClock.timeZone = "Europe/London"
binding.imageView.setImageResource(R.drawable.london)
}
binding.radioButtonNewYork.id -> {
binding.textClock.timeZone = "America/New_York"
binding.imageView.setImageResource(R.drawable.newyork)
}
binding.radioButtonBaijing.id -> {
binding.textClock.timeZone = "CST6CDT"
binding.imageView.setImageResource(R.drawable.baijing)
}
}
but this doesn't work the image doesn't change. So what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用
binding.radiogroup.checkedradiobuttonid
当我应该使用binding.radiogroup.setononcheckedchangelistener {_,checkedid--&gt;
时,我做了更改解决问题时。I used
binding.radioGroup.checkedRadioButtonId
when I should have usedbinding.radioGroup.setOnCheckedChangeListener { _, checkedId ->
, when I did the change it solved the problem.使用 onClickListener 而不是 OnCheckedChangeListener
use onClickListener instead of OnCheckedChangeListener