2015-09-09 21 views
6

をラジオボタンやチェックボックスの色を変更します。しかし、今ではラジオボタンの色とチェックボックスの色を変更したいと思います。私はアンドロイド:私はプログラム的に<code>LinearLayout</code>で<code>RadioButton</code>と<code>CheckBox</code>を作成したプログラムで

RadioButton.setHighlightColor(Color.parseColor("#0c83bd"));

checkbox.setHighlightColor(Color.parseColor("#0c83bd"));

を使用ししかし、それは動作しませんでした。

+0

用背景画像 – Androider

+0

しかし、私は、画像 –

+0

を添付していない色を設定したいアンドロイドサポートライブラリで利用可能な任意の関数はありますか? – Androider

答えて

10

代わりのCheckBoxのRadioButton

RadioGroup newRadioButton = new RadioGroup(this); 
CheckBox newCheckBox = new CheckBox(this); 
0
RadioButton rb=(RadioButton) findViewById(R.id.radioButton1); 
    rb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      // TODO Auto-generated method stub 
      rb.setButtonDrawable(R.drawable.image); 
      rb.setHighlightColor(Color.parseColor("#0c83bd")); 


     } 
    }); 
    } 
12

使用AppCompatCheckBoxAppCompatRadioButtonのこの

AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this); 
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this); 

Instedを試してみてください。 あなたのXMLがあります:Javaコードのために今すぐ

<android.support.v7.widget.AppCompatCheckBox 
    android:id="@+id/cbSelected" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:backgroundTint="@color/colorAccent" //This to set your default button color 
    android:checked="true"/> 

<android.support.v7.widget.AppCompatRadioButton 
    android:id="@+id/rb" 
    app:buttonTint="@color/colorAccent" //This to set your default button color 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

: はのためのプログラムで色を変更するにはAppCompatRadioButtonまたはAppCompatCheckBoxコールsetSupportButtonTintListをColorStateList

 ColorStateList colorStateList = new ColorStateList(
       new int[][]{ 
         new int[]{android.R.attr.state_enabled} //enabled 
       }, 
       new int[] {getResources().getColor(R.color.colorPrimary) } 
     ); 

を作成

AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb); 
radioButton.setSupportButtonTintList(colorStateList); 

AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected); 
cbSelected.setSupportButtonTintList(colorStateList); 
+0

これは受け入れられた回答である必要があります – ScruffyFox

0

CheckBoxについては、お使いのCheckBoxAppCompatCheckBoxと呼び、以下の方法で置き換える:

public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) { 
    ColorStateList colorStateList = new ColorStateList(
      new int[][] { 
        new int[] { -android.R.attr.state_checked }, // unchecked 
        new int[] { android.R.attr.state_checked } // checked 
      }, 
      new int[] { 
        uncheckedColor, 
        checkedColor 
      } 
    ); 
    checkBox.setSupportButtonTintList(colorStateList); 
} 

私はそれがRadioButtonについても同様であるべきだと思います。宣言された属性をandroid.R.attrにチェックしてコードを変更することができます。

6

あなたはこのような動的RadioButtonを作成することができます。

RadioButton male = new RadioButton(ReservationContact.this); 
male.setTextColor(Color.BLACK); 
male.setText("Male"); 
male.setSelected(true); 
male.setId(0); 

これはRadioButtonの円のデフォルトの色を変更するために使用されています。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background))); 
} 
male.setHighlightColor(getResources().getColor(R.color.background)); 
+0

答えとしてマークする必要があります。どうもありがとう –

関連する問題