通过选中多个复选框,从复选框图像访问图像视图的 scr



我正在尝试通过访问复选框中的图像源来更改图像视图中的图像。这是我的 xml 代码的一部分

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myimage"
android:id="@+id/img"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/image1"
android:id="@+id/q4op1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/images2"
android:id="@+id/q4op2"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/images"
android:id="@+id/q4op3"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/download"
android:id="@+id/q4op4"/>

我想检查复选框是否被选中。 如果选中,则该复选框的可绘制权限中的图像应出现在图像视图中。

你能指导我如何为此编写 java 代码吗?

我认为这可能会解决您的问题。下面的代码将为您提供所有可绘制对象。

Drawable[] drawables = textView.getCompoundDrawables();

现在,如果您想比较,则可以使用

Bitmap bmp1 = ((BitmapDrawable)drawables[1] ).getBitmap();
Bitmap bmp2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.twt_hover)).getBitmap();
if(bmp1 == bmp2)
{
//Code block
}

在您的情况下

// Left(0), top(1), right(2), bottom(3) drawables.
// This is the right drawable.
Drawable rightCompoundDrawable = drawables[2];

用于检查复选框是否已选中

cb = (CheckBox) rowView.findViewById(R.id.q4op1); 
if(cb.isChecked()) {
// do your drawable task here
}

最新更新