在Android中检查应用的drawable

时间:2016-06-06 08:30:25

标签: android

我想查看图片按钮(btn_a),如果它包含selected_imageunselected_image

然后,我想通过点击图片按钮将selected_image更改为unselect_imageunselect_image更改为selected_image

到目前为止我的代码:

        btn_a = (ImageButton) convertView.findViewById(R.id.button_a);
    btn_a.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(btn_a.getResources().getDrawable(R.id.button_a)==R.drawable.unselected_image){
                btn_a.setImageResource(R.drawable.selected_image);
                }else{
                btn_a.setImageResource(R.drawable.unselected_image);
                }

        }
    });

但这显示错误btn_a.getResources().getDrawable(R.id.button_a)==R.drawable.unselected_image

帮助我正确实现这一点。

编辑:

@Override
public View getView(int position, View convertView, ViewGroup parent) {     
    if (convertView == null) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_view_cell, null);
    }
    btn_a = (ImageButton) convertView.findViewById(R.id.button_a);
    btn_a.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Drawable drawable = favourite.getDrawable();
            if(drawable.getConstantState().equals(context.getResources().getDrawable(R.drawable.selected_image).getConstantState())){
                btn_a.setImageResource(R.drawable.unselected_image);
            }else{
                btn_a.setImageResource(R.drawable.selected_image);
            }
    return convertView;
}

2 个答案:

答案 0 :(得分:0)

  

试试这个

嘿首先创建上下文对象

在你的班级全球宣布这个

Context context

然后添加这个

btn_a = (ImageButton) convertView.findViewById(R.id.button_a);
btn_a.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
      Drawable drawable = btn_a.getDrawable();
     if (drawable.getConstantState().equals(context.getResources().getDrawable(R.drawable.unselected_image).getConstantState())){
   //Do your work here
      }
   }
});

答案 1 :(得分:0)

试试这个:

    btn_a = (ImageButton) convertView.findViewById(R.id.button_a);
    btn_a.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v)
        {
            Bitmap b1 = ((BitmapDrawable)btn_a.getDrawable()).getBitmap();
            Bitmap unselected_image = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.unselected_image)).getBitmap();
            Bitmap selected_image = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.selected_image)).getBitmap();
             // You will need minimum API level as 12 for using this sameAs function
            if(b1.sameAs(unselected_image))
            {
                btn_a.setImageResource(R.drawable.selected_image);
            }else{
                btn_a.setImageResource(R.drawable.unselected_image);
            }

        }
    });

这将解决您的问题。 我在这里比较位图 .. !!

  

编辑1:

声明一个整数ArrayList:

 ArrayList<Integer> favList = new ArrayList<>();

在构造函数中初始化它:

    for (int i = 0; i < list.size() ; i++) {
        favList.add(R.drawable.unselected_image);
    }

像这样更改你的getView:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_view_cell, null);
    }
    btn_a = (ImageButton) convertView.findViewById(R.id.button_a);
    btn_a.setTag(position);
    btn_a.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Drawable drawable = favourite.getDrawable();
            if (drawable.getConstantState().equals(context.getResources().getDrawable(R.drawable.selected_image).getConstantState())) {
                favList.set((Integer)v.getTag(),R.drawable.unselected_image);
            } else {
                favList.set((Integer)v.getTag(),R.drawable.selected_image);
            }
            notifyDataSetChanged();

        }
    }
    favourite.setImageResource(favList.get(position));
    return convertView;
}