2016-12-21 62 views
0

我有一個既有文字又有圖像的單選按鈕。我試圖改變圖像的顏色,當按鈕被點擊時,通過在它上面應用一種顏色,如色調。我嘗試使用android:state_checked="true",但它不允許我在圖像上應用顏色。單擊/按下時更改可繪製Radiobutton圖像的顏色

我也試圖做到這一點的編程:

RadioButton radioButtonShare= (RadioButton) findViewById(R.id.image_share); 
radioButtonShare.getButtonDrawable().setColorFilter(getResources().getColor(R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP); 

但它與nullpointexception崩潰...

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference 

請協助

單選

<RadioButton 
android:id="@+id/image_share" 
style="@style/style_radiobutton" 
android:layout_height="match_parent" 
android:drawableTop="@drawable/selector_image_share" 
android:text="@string/edit_share" /> 

@ drawabl E/selector_image_share

<item android:drawable="@drawable/viewer_ic_share" android:state_checked="true" /> 
<item android:drawable="@drawable/viewer_ic_share" android:state_pressed="true" /> 
<item android:drawable="@drawable/viewer_ic_share" /> 

+0

如果說空指針異常,然後檢查R.id.image_share無論是在當前的顯示佈局文件 –

+0

@ Mr.Popular它是正確的文件... – Bmbariah

回答

0

我想下面的代碼可以幫助您:下面的代碼

粘貼到你的style.xml文件

style.xml

<style name="radionbutton" 
    parent="Base.Widget.AppCompat.CompoundButton.RadioButton"> 
    <item name="android:button">@android:color/transparent</item> 
    <item name="android:drawableTop">@drawable/radiobutton_drawable</item> 
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowIsFloating">false</item> 
    <item name="android:backgroundDimEnabled">true</item> 
</style> 

在中創建radiobutton_drawable.xml文件您的可繪製文件夾並在radiobutton_drawable.xml文件中粘貼以下代碼。

radiobutton_drawable.xml

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

粘貼以下代碼activity_main.xml中的文件。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    > 

    <RadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <RadioButton 
      android:id="@+id/radio2" 
      style="@style/radionbutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="male" /> 

     <RadioButton 
      style="@style/radionbutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/radio2`enter code here`" 
      android:text="female" /> 
    </RadioGroup> 
</RelativeLayout>