2016-12-21 10 views
0

テキストと画像の両方を持つラジオボタンがあります。私は色のようにその上に色を適用してボタンをクリックすると、画像の色を変更しようとしています。私はandroid:state_checked="true"を使ってみましたが、イメージの上にカラーを適用することはできません。クリック/押したときのラジオボタン画像の描画色の変更

私はまたしてプログラム的にそれを実行しようとしました:

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

<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" /> 

を支援@ドレイブル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

あなたがnullポインタ例外が、それはそれが正しいファイルであるMr.Popular @現在上映レイアウトファイル –

+0

にあるかどうかを確認してくださいR.id.image_share言っている場合... – 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ファイルをdrawableフォルダとradiobutton_drawable.xmlファイルの下にコードを貼り付けます。 activity_main.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

<?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> 
関連する問題