Android: textColor wyłączony przycisk w selektor nie pokazuje?

Próbuję zrobić przycisk z selektorem Mój przycisk może mieć następujące stany:

  • Włączone / Wyłączone
  • Pressed / Not Pressed

Zgodnie z wyżej wymienionymi Stanami. Muszę manipulować przyciskiem:

  • kolor tekstu
  • obraz tła

Przycisk zaczyna się od mojego wyłączenia, więc powinien mieć wyłączone textColor i wyłączone tło przycisku. Ale widzę domyślny textColor (określony w stylu) i żadnego obrazu tła!

Oto mój selektor button_selector.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="false"
        android:state_enabled="false"
        android:textColor="#9D9FA2"
        android:drawable="@drawable/button" />    

    <item android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/button_pressed"/>

    <item android:state_pressed="true"
        android:state_enabled="false"
        android:textColor="#9D9FA2"
        android:drawable="@drawable/button"/>

    <item android:state_pressed="false"
        android:state_enabled="true"
        android:drawable="@drawable/button"/>    

</selector>

A oto moja deklaracja przycisku w moim układzie.xml

    <Button android:id="@+id/reserve_button"
        android:text="@string/reserve_button"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:paddingRight="15dp"
        android:layout_gravity="left"
        style="@style/buttonStyle"
        android:background="@drawable/button_selector" />

I wreszcie to jest mój styl (gdzie mój domyślny textColor jest ustawiony)

<?xml version="1.0" encoding="utf-8"?>

 <resources>

     <style name="buttonStyle">
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">#282780</item>
      <item name="android:textSize">18sp</item>
     </style>

</resources>
Proszę o pomoc!
Author: Nouran H, 2012-06-27

5 answers

Należy również utworzyć ColorStateList dla kolorów tekstu identyfikujących różne stany.

Wykonaj następujące czynności:

  1. Utwórz kolejny plik XML w res\color o nazwie text_color.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <!-- disabled state -->
      <item android:state_enabled="false" android:color="#9D9FA2" /> 
      <item android:color="#000"/>
    </selector>
    
  2. W Twoim style.xml Umieść odniesienie do tego text_color.xml pliku w następujący sposób:

    <style name="buttonStyle" parent="@android:style/Widget.Button">
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">@color/text_color</item>
      <item name="android:textSize">18sp</item>
    </style>
    
To powinno rozwiązać twój problem.
 194
Author: Adil Soomro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-01 23:41:55

Najprostszym rozwiązaniem jest ustawienie filtra kolorów na obrazku tła i przycisku jak zobaczyłem Tutaj

Możesz wykonać następujące czynności:

if ('need to set button disable')
    button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
else
    button.getBackground().setColorFilter(null);
Mam nadzieję, że komuś pomogłem...
 4
Author: Woody,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:24

1.Utwórz folder kolorów w /Res / folder i w folderze kolorów utwórz na xml:

Text_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- disabled state --> 
<item android:state_enabled="false" android:color="#776678" /> 
 <item android:color="#ffffff"/>
</selector>

2.Teraz Utwórz układ xml: -

 <Button

        android:id="@+id/button_search"

        android:layout_width="652dp"

        android:layout_height="48dp"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="18dp"

        android:background="@android:color/transparent"

        android:text="Hello Bhaskar"

        android:textColor="@color/text_color_selector"/>  
 3
Author: Bhaskar Kumar Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-09 10:30:55
<Button android:id="@+id/reserve_button"
        android:text="@string/reserve_button"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:paddingRight="15dp"
        android:layout_gravity="left"
        style="@style/buttonStyle"
        android:background="@drawable/button_selector" />

Nie widzę diabling twojego przycisku w układzie XML. dodaj to do układu przycisków.

android:enabled="false"

Więc twój układ przycisków będzie,

<Button android:id="@+id/reserve_button"
        android:text="@string/reserve_button"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:enabled="false"
        android:paddingRight="15dp"
        android:layout_gravity="left"
        style="@style/buttonStyle"
        android:background="@drawable/button_selector" />
 0
Author: Aqif Hamid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-27 11:40:01
<item android:state_enabled="true"> </item>

<item android:state_enabled="false"> </item>

<item android:state_pressed="true" > </item>
Mam nadzieję, że zadziała!
 0
Author: Lal Krishna,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-16 06:37:36