Android изменить цвет круга и textColor радиокнопки - PullRequest
0 голосов
/ 05 мая 2020

Когда я нажимаю переключатель, я хочу, чтобы цвет текста и цвет круга изменились одновременно с в изображение ниже.

enter image description here

ПРИМЕЧАНИЕ

Выбранный цвет круга и текст не имеет цвета colorPrimary или colorAccent.

Ответы [ 3 ]

1 голос
/ 05 мая 2020

Если вы хотите изменить текст и цвет кругов одновременно, выполните следующие действия.

1. добавьте стиль ниже

<style name="radioButton" parent="AppTheme">
    <item name="colorControlNormal">#0f0</item>
    <item name="colorAccent">#f00</item>
  </style>

2. добавьте этот селектор. xml для рисования

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="#0f0" android:state_pressed="true" />
  <item android:color="#f00" android:state_checked="true" />
  <item android:color="#0f0" />
</selector>

3. добавить селектор и стиль в RadioButton

 <RadioButton
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:text="select"
            android:textColor="@drawable/selector"
            android:textSize="12sp"
            android:theme="@style/radioButton"
            app:useMaterialThemeColors="false" />

ПОЛНЫЙ КОД:

<RadioGroup
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="left"
          android:layout_margin="16dp"
          android:checkedButton="@+id/one"
          android:layoutDirection="ltr"
          android:orientation="horizontal">

          <RadioButton
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:text="select"
            android:textColor="@drawable/selector"
            android:textSize="12sp"
            android:theme="@style/radioButton"
            app:useMaterialThemeColors="false" />

          <RadioButton
            android:id="@+id/two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="not select"
            android:textColor="@drawable/selector"
            android:textSize="12sp"
            android:theme="@style/radioButton"
            app:useMaterialThemeColors="false" />
        </RadioGroup>

И результат

enter image description here

Примечание: Я использовал материалы Google

0 голосов
/ 05 мая 2020

MaterialRadioButton использует по умолчанию селектор, в котором цвета основаны на colorOnSurface, когда unchecked и colorControlActivated, когда checked определено в теме вашего приложения.

Вы можете переопределить их, используя атрибут android:theme. Что-то вроде:

<com.google.android.material.radiobutton.MaterialRadioButton
    ...
    android:theme="@style/ThemeOverlay.RadioButton"/>

с:

  <style name="ThemeOverlay.RadioButton" parent="">
    <item name="colorControlActivated">@color/.....</item>
    <item name="colorOnSurface">@color/.....</item>
    <item name="colorSurface">@color/.....</item>
  </style>

Вы также можете установить атрибут useMaterialThemeColors на false

<com.google.android.material.radiobutton.MaterialRadioButton
    ...
    useMaterialThemeColors="false"/>

и применить настраиваемый селектор используя CompoundButtonCompat#setButtonTintList.

с таким селектором, как:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="..." android:state_enabled="true" android:state_checked="true"  />
  <item android:color="..." android:state_enabled="true" android:state_checked="false" />
  <item android:color="..." android:state_enabled="false" android:state_checked="true"  />
  <item android:color="..." android:state_enabled="false" android:state_checked="false" />  
</selector>
0 голосов
/ 05 мая 2020
radiobutton.setOnCheckedChangeListener { buttonView, isChecked ->
        if(radiobutton.isChecked())
        {
            // Changing radio button 1 color on checked.
            radiobutton.setTextColor(Color.parseColor("#00ff00"))}}
...