【问题标题】:Access scr for ImageView from CheckBox image by checking more than one CheckBox通过检查多个 CheckBox 从 CheckBox 图像访问 ImageView 的 scr
【发布时间】:2018-01-13 07:35:08
【问题描述】:

我正在尝试通过访问 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代码吗?

【问题讨论】:

  • cb = (CheckBox) rowView.findViewById(R.id.checkbox1); if(cb.isChecked()) { .... }
  • 是的,先生,我知道 isChecked 函数,但如何从特定复选框访问图像的引用
  • 你的意思是你有多个图像?为每个复选框确定哪个是哪个?
  • 是的先生,图像在每个复选框的右侧
  • 但您的 XML 布局中有一个图像视图。要么有多个不同的 id,如 image1,image2,...,要么动态添加它们。

标签: android android-imageview android-checkbox


【解决方案1】:

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

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2016-02-27
    • 2018-10-01
    相关资源
    最近更新 更多