2015-09-09 12 views
6

Utworzono RadioButton i w . Ale teraz chcę zmienić kolor przycisku i kolor pól wyboru. UżywamAndroid: Programowo programowo zmieniaj kolor przycisków RadioButtons i pól wyboru

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

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

Ale to nie działa.

+0

tle wykorzystania zdjęć – Androider

+0

ale chcę, aby ustawić kolor, aby nie dołączyć obrazy –

+0

jest jakaś funkcja dostępna w Android biblioteki wsparcia? – Androider

Odpowiedz

10

Spróbuj

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

insted

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

Zastosowanie AppCompatCheckBox i AppCompatRadioButton zamiast CheckBox i RadioButton. Twój xml będą miały:

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

Teraz dla kodu Java: Tworzenie ColorStateList

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

Aby zmienić kolor programowo dla AppCompatRadioButton lub AppCompatCheckBox wezwanie setSupportButtonTintList .

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

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

Powinna to być zaakceptowana odpowiedź. – ScruffyFox

0

Dla CheckBox, wymień CheckBox z AppCompatCheckBox i połączeń następujący sposób:

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); 
} 

myślę, że powinno być podobne do RadioButton. Możesz sprawdzić zadeklarowane atrybuty w android.R.attr i zmienić kod.

6

Można tworzyć dynamiczne RadioButton takiego:

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

ten służy do zmiany domyślnego koloru kręgu 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

Powinna oznaczać jako odpowiedź. Wielkie dzięki –

Powiązane problemy