如果我们选中了android中的复选框,如何更改复选框背景

时间:2015-01-22 07:36:44

标签: android

这是我的班级

public class AdapterContact extends BaseAdapter {
        private Context context;
        private ArrayList<Datamodel> arrModel;
        public ArrayList<Datamodel> arrSelectedModel;
        // private ArrayList<Boolean> arrCheckboxState;
        ViewHolder holder;
        ImageLoader imageloader;

        public AdapterContact(Context context, ArrayList<Datamodel> arrModel) {
            this.context = context;
            this.arrModel = arrModel;
            arrSelectedModel = new ArrayList<Datamodel>();
            imageloader = new ImageLoader(context);

        }

        @Override
        public int getCount() {
            return arrModel.size();
        }

        @Override
        public Object getItem(int position) {
            return arrModel.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                holder = new ViewHolder();
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.row_contact, null);
                holder.checkBox = (CheckBox) convertView
                        .findViewById(R.id.rowcontact_checkbox);
                holder.txtContactName = (TextView) convertView
                        .findViewById(R.id.rowcontact_txtName);
                holder.imgProfilepic = (ImageView) convertView
                        .findViewById(R.id.rowcontact_imgProfile);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.txtContactName.setText(arrModel.get(position).contactName);

            holder.checkBox
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            int position = (Integer) buttonView.getTag();

                            if (isChecked) {
                                // arrCheckboxState.set(position, true);
                                arrModel.get(position).setChecked(true);
                                arrSelectedModel.add(arrModel.get(position));
                            } else {
                                arrModel.get(position).setChecked(false);
                                // arrCheckboxState.set(position, false);
                                arrSelectedModel.remove(arrModel.get(position));
                            }
                        }
                    });
            holder.checkBox.setTag(position);
            holder.checkBox.setChecked(arrModel.get(position).isChecked);
            if (arrModel.get(position).isActive) {
                holder.checkBox.setVisibility(View.GONE);
            }

            AQuery aq = new AQuery(context);
            aq.id(holder.imgProfilepic).image(arrModel.get(position).picturePath,
                    false, false, 0, R.drawable.imge);
            String url2 = arrModel.get(position).picturePath + "?t="
                    + System.currentTimeMillis();
            aq.id(holder.imgProfilepic).image(url2, false, false, 0,
                    R.drawable.imge);
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
            return convertView;
        }

        class ViewHolder {
            TextView txtContactName;
            ImageView imgProfilepic;
            CheckBox checkBox;
        }
    }

这是我的XMl:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dip"
    android:background="#ffffff"
    android:orientation="vertical" >

    <com.lociiapp.utils.RoundedImageView
        android:id="@+id/rowcontact_imgProfile"
        android:layout_width="46dip"
        android:layout_height="46dip"
        android:layout_centerVertical="true"
        android:layout_marginBottom="7dip"
        android:layout_marginLeft="7dip"
        android:layout_marginTop="7dip" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dip"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/rowcontact_imgIcon"
        android:layout_toRightOf="@+id/rowcontact_imgProfile"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/rowcontact_txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginTop="17dip"
            android:text="Usama Sadiq"
            android:textColor="#3c3b3b"
            android:textSize="17dip" />
    </RelativeLayout>

    <CheckBox
        android:id="@+id/rowcontact_checkbox"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:button="@drawable/ic_launcher" />

</RelativeLayout>

我正在列表视图中打印数据,当未选中项目时选中复选框或dfault然后复选框背景图像将为x,如果我们选中或启用任何项目的复选框,则背景图像应该更改我绑定到xml中的set dfault图像检查后背景不变,请检查代码在哪里做错了。 rowcontact_checkbox id复选框请建议我

2 个答案:

答案 0 :(得分:3)

使用此CHECKBOX SELECTOR

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/>
 <!-- checked -->
    <item android:drawable="@drawable/checkbox_unchecked" android:state_checked="false"/>
 <!-- unchecked -->

</selector>

并为UR CHECKBOX添加这些线路

android:background="@drawable/checkbox_selector"
android:button="@android:color/transparent"

将此代码用于适配器

中的listview
lstview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterview, View view,
                    int position, long id) {

                CheckBox chkbox = (CheckBox) view
                        .findViewById(R.id.chkboxid);
                chkbox.performClick();

            }
        });

答案 1 :(得分:1)

将您的复选框背景设置为selector,将其置于drawable/your_background.xml下。选中/取消选中/禁用复选框后,selector将决定显示的背景等...您可以尝试android-holo-colors快速生成一个

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:drawable="@drawable/your_checked_background" />
    <item android:drawable="@drawable/your_unchecked_background" />
</selector>