2012-06-27 12 views
63

tôi đang cố gắng để tạo ra một nút với một chọn nút của tôi có thể có tình trạng sau:Android: textColor nút tắt trong bộ chọn không hiển thị?

  • Enabled/Disabled
  • ép/Không Pressed

Theo các tiểu bang nêu trên. Tôi cần phải thao tác của nút:

  • Màu chữ
  • hình nền

Nút bắt đầu giảm giá của tôi bị vô hiệu hoá vì vậy nó phải có Format tàn tật và nền nút khuyết tật. Nhưng tôi có thể thấy textColor mặc định (được chỉ định theo kiểu) và NO background image!

Đây là selector tôi 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> 

Và đây là lời tuyên bố nút của tôi trong Layout.xml tôi

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

Và cuối cùng đây là phong cách của tôi (nơi Format mặc định của tôi được thiết lập)

<?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> 

Xin vui lòng trợ giúp!

Trả lời

175

Bạn cũng cần phải tạo ra một ColorStateList cho màu sắc văn bản xác định trạng thái khác nhau.

Làm như sau:

  1. Tạo một tập tin XML trong res\color tên giống như 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. Trong style.xml của bạn, đặt một tham chiếu đến mà text_color.xml tập tin như sau:

    <style name="buttonStyle"> 
        <item name="android:textStyle">bold</item> 
        <item name="android:textColor">@color/text_color</item> 
        <item name="android:textSize">18sp</item> 
    </style> 
    

này nên giải quyết vấn đề của bạn.

+1

cảm ơn bạn rất nhiều! Chính xác những gì tôi cần! :) –

+1

bộ chọn, yay! – speedynomads

+1

bạn không cần phải lưu tệp text_color.xml của bạn trong/res/drawable (thay vì/res/color) nếu bạn tham chiếu đến nó bằng @ drawable/text_color? – Erwan

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

Tôi không thể nhìn thấy nút của bạn bị xáo trộn trong bố cục xml. thêm điều này vào bố cục nút của bạn.

android:enabled="false" 

nên bố trí nút của bạn sẽ được,

<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

@ Aqif Hamid Tôi làm điều đó một cách có lập trình button.setEnabled (sai) trong phương thức onCreat() –

+0

@NouranH Hãy chia sẻ mã của bạn. –

4

Các giải pháp dễ dàng nhất là để thiết lập bộ lọc màu cho hình nền và nút như tôi thấy here

Bạn có thể làm như sau:

if ('need to set button disable') 
    button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY); 
else 
    button.getBackground().setColorFilter(null); 

Hope tôi đã giúp một ai đó ...

+0

Đây là một cách hay để làm điều này một cách năng động, khi bạn không có hình nền của nút trước. Nhưng điều này không quan tâm đến màu văn bản. –

3

1.Tạo thư mục màu trong/res/folder và trong thư mục màu tạo trên 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.Now Tạo một bố cục 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"/> 
0
<item android:state_enabled="true"> </item> 

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

<item android:state_pressed="true" > </item> 

Hy vọng nó sẽ hoạt động!