简体   繁体   English

在Android中检查已应用的可绘制对象

[英]Checking applied drawable in Android

I want to check the Image button ( btn_a ), if it contain selected_image or unselected_image . 我想检查图像按钮( btn_a ),如果它包含selected_imageunselected_image

Then I want to change the selected_image to unselect_image or unselect_image to selected_image by the click of the Image Button. 然后我想改变selected_imageunselect_imageunselect_imageselected_image由图像按钮的点击。

My code so far: 到目前为止,我的代码:

        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);
                }

        }
    });

But this shows an error btn_a.getResources().getDrawable(R.id.button_a)==R.drawable.unselected_image . 但这显示了错误btn_a.getResources().getDrawable(R.id.button_a)==R.drawable.unselected_image

Help me to implement this correctly. 帮助我正确实现此目的。

EDIT : 编辑:

@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;
}

Try this 尝试这个

hey Create context object first 嘿首先创建上下文对象

In your Class Globally Declare this 在您的班级中全局声明

Context context

then After add thiss 然后在添加这个之后

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
      }
   }
});

Try this: 尝试这个:

    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);
            }

        }
    });

This will solve your problem. 这样可以解决您的问题。 I am comparing bitmaps here ..!! 我在这里比较位图 .. !!

EDIT 1: 编辑1:

Declare an Integer ArrayList : 声明一个整数ArrayList:

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

Initialize it in your constructor : 在构造函数中初始化它:

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

Change your getView like this: 像这样更改您的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM