2011-11-16 33 views
18

Tôi có một số Buttons trên ứng dụng Android của mình. Họ có một biểu tượng và văn bản. Tôi có thể thiết lập màu nền của Button trong mã java. Nếu nút được nhấp, tôi muốn hiển thị với màu khác. Vì vậy, làm thế nào để tôi thiết lập một màu sắc khác nhau cho trạng thái ép của Button?Làm cách nào để đặt màu khác cho trạng thái nhấn của nút?

<Button 
    android:id="@+id/save" 
    android:layout_width="130dip" 
    android:layout_height="wrap_content" 
    android:scaleType="center" 
    android:drawableTop="@drawable/save" 
    android:text="Save" 
    android:textColor="#FFFFFF" 
    android:textSize="14dip" 
> 

Các onCreate phương pháp:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.homescreen); 
save = (Button)findViewById(R.id.save); 
    save.setBackgroundColor(Color.rgb(27,161,226)); } 
+0

http://stackoverflow.com/questions/4755871/how-to-set-image-button-backgroundimage-for-different-state/4755934#4755934 – ingsaurabh

Trả lời

39

tạo file xml bằng cách sử dụng hình ảnh nút như thế này với mybutton.xml trong thư mục drawable

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@color/greencolor" /> 
    <item android:state_focused="true" android:drawable="@color/yellowcolor" /> 
    <item android:drawable="@color/redcolor" /> 
</selector> 

và sử dụng trong nút mã XML

android:background="@drawable/mybutton" 

thêm các mã màu đó vào ource -> giá trị -> colors.xml như thế này

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="blue">#0066cc</color> 
    <color name="gold">#e6b121</color> 
</resources> 

tham khảo: Change button background on touch

+0

Cảm ơn câu trả lời của bạn nhưng tôi không sử dụng hình ảnh cho nút. Tôi muốn thiết lập màu nền của nút khi nó nhấn – realuser

+0

, chúng tôi có thể thay đổi màu nút bằng cách sử dụng ... –

+0

Cảm ơn bạn rất nhiều PolamReddyRajaReddy – realuser

2

Sử dụng một StateList. Dưới đây là một ví dụ về một chọn với một drawable khác nhau cho tình trạng ép:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/drawable_for_pressed_state" android:state_pressed="true"/> 
    <item android:drawable="@drawable/drawable_for_normal_state"/> 

</selector> 
5

Dưới đây là đoạn code mẫu cho danh sách trạng thái màu sắc sử dụng cho một nút

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

    <selector xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item 
      android:color="#c0c0c0" 
      android:state_selected="true"/> 
     <item 
      android:color="#ffffff" 
      android:state_pressed="true"/> 
     <item 
      android:color="#9A9A9A" 
      android:state_focused="false" 
      android:state_pressed="false" 
      android:state_selected="false"/> 
</selector> 

Ngoài ra hãy kiểm tra bên dưới liên kết cho màu danh sách nhà nước

http://developer.android.com/guide/topics/resources/color-list-resource.html

+2

thẻ yêu cầu một 'drawable' thuộc tính hoặc thẻ con xác định một drawable –

+0

@IgorGanapolsky đây là một bộ chọn màu, không phải là drawable - nó đi vào res/color – Tom

3

Bạn cần phải sử dụng một drawable với selector cho các nước ép, thường được thực hiện trong các liên kết xml bên dưới.

http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

+0

Công việc doen't này, code twrows exexception 'org.xmlpull.v1.XmlPullParserException: Dòng tệp nhị phân XML # 6: thẻ yêu cầu thuộc tính 'drawable' hoặc thẻ con xác định drawable' – HitOdessit

-3

Nếu bạn muốn thay đổi màu nền nút sau đó chỉ cần làm như sau ..

@Override 
    public void onClick(View v) {  
     if(v.getId() == R.id.btn01) { 
       btn1.setBackgroundColor(Color.RED); 
       btn1.setTextColor(Color.WHITE); 

      } 

chỉ thêm mã này vào sự kiện onclick của nút.

0

edittext_modified_states.xml

<?xml version="1.0" encoding="UTF-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/apptheme_textfield_activated_holo_light" /> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/apptheme_textfield_focused_holo_light" /> 
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/apptheme_textfield_disabled_focused_holo_light"/> 
    <item android:drawable="@drawable/apptheme_textfield_default_holo_light" /> 
</selector> 

đây: http://android-holo-colors.com goto trang web này và chọn màu sắc của bạn và imort vào drawable của bạn. goto bố trí xml và thiết lập nút nền. android: background = "@ drawable/edittext_modified_states"

Các vấn đề liên quan