选中的复选框在片段中变为黄色

时间:2016-05-03 13:07:50

标签: android listview android-fragments android-studio android-checkbox

我在listview中带有片段的复选框有问题。

为什么当我点击复选框时这是黄色的?我看不到支票符号;在活动中它是好的....我给你看截图。

我希望你能帮助我!

由于

enter image description here

CODE XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <CheckBox 
        android:id="@+id/chk_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" />
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:orientation="vertical"
        android:layout_weight="1" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/chk_box"
        android:textStyle="bold" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/dist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_toRightOf="@id/chk_box"
        android:textSize="12sp"
        android:textStyle="italic" />
        <TextView
            android:id="@+id/valuta"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textSize="12sp"
            android:textStyle="italic" />
    </LinearLayout>
    <EditText
        android:layout_width="196dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText"
        android:hint="quantità"
        >
    </EditText>
    </LinearLayout>

</LinearLayout>

适配器代码:

class PlanetAdapter extends ArrayAdapter<Planet> implements CompoundButton.OnCheckedChangeListener

{

    private List<Planet> planetList;
    private Context context;
    ArrayList<Planet> objects;


    public  PlanetAdapter(List<Planet> planetList, Context context) {
        super(context, R.layout.single_listview_item, planetList);
        this.planetList = planetList;
        this.context = context;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }


    public  class PlanetHolder  {
        public TextView planetName;
        public TextView distView;
        public TextView valuta;
        public CheckBox chkBox;
        public EditText edit;
        public String quantità;



    }

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

View row = convertView;
        PlanetHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(R.layout.single_listview_item, parent, false);
            holder = new PlanetHolder();
            holder.planetName = (TextView) row.findViewById(R.id.name);
            holder.distView = (TextView) row.findViewById(R.id.dist);
            holder.valuta = (TextView) row.findViewById(R.id.valuta);
            holder.chkBox = (CheckBox) row.findViewById(R.id.chk_box);
            holder.edit = (EditText) row.findViewById(R.id.editText);
            holder.edit.setVisibility(View.GONE);
            holder.edit.setEnabled(false);
            row.setTag(holder);
        } else {
            holder = (PlanetHolder) row.getTag();
        }
        final Planet p = planetList.get(position);

        holder.chkBox.setOnCheckedChangeListener(PlanetAdapter.this);
        final PlanetHolder finalHolder = holder;
        holder.chkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (finalHolder.chkBox.isChecked()) {
                    finalHolder.edit.setVisibility(View.VISIBLE);
                    finalHolder.edit.setEnabled(true);
                    finalHolder.edit.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            p.setQuantità(finalHolder.edit.getText().toString().trim());

                        }
                    });
                } else {
                    finalHolder.edit.setVisibility(View.GONE);
                    finalHolder.edit.setEnabled(false);
                    finalHolder.edit.setText(null);
                }
            }
        });
        holder.planetName.setText(p.getName());
        holder.distView.setText("" + p.getDistance());
        holder.valuta.setText(""+p.getValuta());
        holder.chkBox.setChecked(p.isSelected());
        holder.chkBox.setTag(p);
        holder.edit.setEnabled(false);

        return row;
    }


    ArrayList<Planet> getBox() {
        ArrayList<Planet> box = new ArrayList<Planet>();
        for (Planet p : planetList) {
            if (p.selected)
                box.add(p);
        }
        return box;
    }


}

1 个答案:

答案 0 :(得分:0)

您可以设置这样的复选框布局,希望这对您有帮助。

<CheckBox 
        android:id="@+id/chk_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:button="@drawable/custom_checkbox"
        android:layout_alignParentLeft="true" />

并且您的自定义复选框xml文件是

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:drawable="@drawable/checked" />
    <item android:state_pressed="true"
        android:drawable="@drawable/upchecked" />
    <item android:drawable="@drawable/upchecked" />
</selector>

并选中或取消选中您想要设置的图像。您访问此http://androidexample.com/Custom_Checkbox_With_The_Use_Of_Selectors_And_Shapes/index.php?view=article_discription&aid=80

的更多信息