在RadioGroup中单击单选按钮时更改ImageView背景

时间:2014-10-23 01:54:19

标签: android android-imageview android-radiogroup

在我的应用程序中,我有两个单选按钮位于Radio组内,我有单个Image View。两个单选按钮选项命名为太阳和月亮,我的图像视图设置为太阳图像。

当我点击单选按钮名称月亮时,我想将设置图像更改为月亮。我怎么能在广播组内做到这一点?

1 个答案:

答案 0 :(得分:0)

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.[add your id here <---WITHOUT [].]);


int checkedRadioButton = radioGroup.getCheckedRadioButtonId();



switch (checkedRadioButton) {
 case R.id.radiobutton1 : imageview.setBackgroundImage(R.drawable.sun);
                              break;
 case R.id.radiobutton2 : image view.setBackgroundImage(R.drawable.moon);
                          break;
}

注意 - 这只是功能,没有xml。我想你足够聪明,可以把它连接到你的xml。

注2 - 我可能会误以为:setBackgroundImage。可能有点不同。

相关问题