android - 通过选中多个 CheckBox 从 CheckBox 图像访问 ImageView 的 scr

标签 android android-imageview android-checkbox

我试图通过访问 CheckBox 中的图像源来更改 ImageView 中的图像。这是我的部分 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"/>

我想检查 CheckBox 是否被选中。如果选中,则该 CheckBox 的 drawableRight 中的图像应该出现在 ImageView 中。

你能指导我如何为此编写 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
}

关于android - 通过选中多个 CheckBox 从 CheckBox 图像访问 ImageView 的 scr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45522451/

相关文章:

java - ListView 中的复选框具有奇怪的检查行为

java - 单击复选框时显示图像数组列表

android 9-patch 可绘制xml

android - SDK版本的Android Gradle问题

java - android.app.SuperNotCalledException : Activity did not call through to super. onCreate()

android - 如何在 RelativeLayout 中均匀分布我的 ImageView

android - 键盘出现时隐藏ImageView,键盘消失时显示

android - SendMail 执行中的应用程序崩溃

android动画将圆形图像向各个方向扩展

关于 CheckBox 和 RadioButton 的 Android xml 布局