【问题标题】:how to changed checkbox background if we checked checbox in android如果我们在android中选中复选框,如何更改复选框背景
【发布时间】:2015-01-22 07:36:43
【问题描述】:

这是我的课

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 中设置 dfault 图像和检查背景没有改变后,请检查代码哪里做错了。复选框的rowcontact_checkbox id请建议我

【问题讨论】:

标签: android


【解决方案1】:

使用此复选框选择器

<?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>

并为您的复选框添加这些行

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

将此代码用于适配器中的列表视图

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

            }
        });

【讨论】:

  • 但我无法隐藏和启用复选框检查了我们必须在我的代码中更改的内容
  • 你想在选中复选框时隐藏它吗?
  • yes snag.gy/ipyWi.jpg 通过默认检查此图像我想设置复选框背景 x 并在检查 y 后再次取消选中 x
  • 我将把这段代码放在 adpater 类中请告诉我因为 listview,setonclick 无法正常工作
  • 它不工作我想检查和取消选中复选框的免费图像应该在列表视图项目上更改请看我的图像你会得到
【解决方案2】:

将您的复选框背景设置为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>

【讨论】:

  • 但我无法隐藏和启用复选框检查了我们必须在我的代码中更改的内容
猜你喜欢
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多