0

我有一個問題與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; 
    } 


} 
+0

您的屏幕截圖不顯示任何黃色。 – F43nd1r

+0

[Android:設置CheckBox的顏色]的可能的副本(http://stackoverflow.com/questions/11464143/android-set-color-of-checkbox) –

+0

如果你看到更好,方塊是黃色....可以' t看到檢查符號... – Markella92

回答

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