2011-10-16 106 views
40

我使用的ListView和每一行我有row_item.xml和我吹在代碼如何變化選擇框

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <CheckBox 
     android:id="@+id/chk" 
     android:layout_alignParentLeft="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/txtChoice" 
     android:textColor="#FF0000" 
     android:text="TEST" 
     android:layout_toRightOf="@id/chk" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 

如何更改複選框使用其他custom_1圖像進行檢查時,另一個默認圖片custom_2圖像何時未選中?

回答

137

可繪製customdrawablecheckbox.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="false" android:drawable="@drawable/unchecked_drawable" /> 
    <item android:state_checked="true" android:drawable="@drawable/checked_drawable" /> 
    <item android:drawable="@drawable/unchecked_drawable" /> <!-- default state --> 
</selector> 

yourcheckbox XML:

<CheckBox 
    android:id="@+id/chk" 
    android:button="@drawable/customdrawablecheckbox" 
    android:layout_alignParentLeft="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+2

謝謝!我完全爲我工作... –

+0

如果我想取消選中的項目選擇器的哪個屬性可以使用.. –

+0

我必須以編程方式生成複選框如何使用自定義複選框任何人有一個想法? – UmAnusorn

9

複選框是一個按鈕,因此您可以提供您自己的繪圖與檢查取消選中狀態,它作爲複選框背景。例如

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="false" android:drawable="@drawable/yourdrawable1" /> 
<item android:state_checked="true" android:drawable="@drawable/yourdrawable2" /> 
<item android:drawable="@drawable/yourdrawable1" /> <!-- default --> 
</selector> 

並把它放在你的可繪製文件夾的file.xml中。在您的複選框:

<CheckBox 
    android:button="@drawable/file" 
    android:id="@+id/chk" 
    android:layout_alignParentLeft="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+1

android:背景不會使用checkbutton圖像運行,它會在背景視圖中放置另一個圖像。 –

+1

你正確.... – Blackbelt

+0

如果我想取消選中的項目選擇器的屬性可以使用.. –

4

它很容易:) 首先,你需要創建一個CustomCheckBox類將擴展CheckBox並覆蓋onDraw(Canvas canvas)方法:

public class CustomCheckBox extends CheckBox { 
private final Drawable buttonDrawable; 

public CustomCheckBox(Context context, AttributeSet set) { 
    super(context, set); 
    buttonDrawable = getResources().getDrawable(R.drawable.custom_check_box); 
    try { 
     setButtonDrawable(android.R.color.transparent); 
    } catch (Exception e) { 
     // DO NOTHING 
    } 
    setPadding(10, 5, 50, 5); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    buttonDrawable.setState(getDrawableState()); 

    final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK; 
    final int height = buttonDrawable.getIntrinsicHeight(); 
    if (buttonDrawable != null) { 
     int y = 0; 

     switch (verticalGravity) { 
     case Gravity.BOTTOM: 
      y = getHeight() - height; 
      break; 
     case Gravity.CENTER_VERTICAL: 
      y = (getHeight() - height)/2; 
      break; 
     } 

     int buttonWidth = buttonDrawable.getIntrinsicWidth(); 
     int buttonLeft = getWidth() - buttonWidth - 5; 
     buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height); 
     buttonDrawable.draw(canvas); 
    } 
} 
} 

還可以創建你的選擇你繪製的文件夾命名爲custom_check_box

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:state_window_focused="false" 
     android:drawable="@drawable/btn_check_on" /> 
    <item android:state_checked="false" android:state_window_focused="false" 
     android:drawable="@drawable/btn_check_off" /> 
    <item android:state_checked="true" android:state_pressed="true" 
     android:drawable="@drawable/btn_check_on" />  
    <item android:state_checked="false" android:state_focused="true" 
     android:drawable="@drawable/btn_check_off" /> 
    <item android:state_checked="false" android:drawable="@drawable/btn_check_off" /> 
    <item android:state_checked="true" android:drawable="@drawable/btn_check_on" /> 
</selector> 

而在上述所有三種狀態(聚焦/壓/默認)的XML

使用您的自定義圖標/ IMGS像這樣在XML中使用自定義組件:

<*package + class path*.CustomCheckBox // example com.mypackage.ui.CustomCheckBox if your project is named "mypackage" and the class is in the "ui" folder 
      android:text="@string/text" 
      android:checked="false" android:layout_width="fill_parent" 
      android:id="@+id/myCheckbox" android:layout_height="wrap_content"/> 

and java:

private CustomCheckBox mCheckbox; 
mCheckbox = (CustomCheckBox) findviewbyid(R.id.myCheckbox); 

它的工作原理是因爲我用它兩種方式:)而且一些調整它也適用於RadioButtons同樣的方式。快樂的編碼!

0

您可以在xml中使用選擇器,該選擇器用於根據選中的狀態動態更改複選框的圖像。

例如:

<item android:drawable="@drawable/ic_linkedin" android:state_checked="true" /> 
<item android:drawable="@drawable/ic_linkedin_disabled" android:state_checked="false" /> 

在下面的文件,如果該複選框被選中,它將設置ic_linkedin圖標,如果該複選框是選中,它將設置ic_linkedin_disabled圖標。