Change the color of the button when pressed in Android

Source: Internet
Author: User

Original source: http://blog.csdn.net/nmsoftklb/article/details/9087233

A, in the development of everyone will encounter this situation, in a xxx.xml file if more than two components have the same attribute function, you can extract their common content

Put in the Styles.xml file to declare.

It can then be referenced in the corresponding component: style= "@style/widget_listview"

The style tag can also be defined like this: res-----values------styles.xml

  
 
  1. <style name="widget_listview">
  2. <item name="android:layout_width">match_parent</item>
  3. <item name="android:layout_height">match_parent</item>
  4. <item name="android:layout_weight">1</item>
  5. /span>< item name = "Android:listselector" > @drawable / listview_item_selector </ item >
  6. <item name="android:divider">@drawable/listview_divider_line</item>
  7. <item name="android:dividerHeight">1.0dip</item>
  8. <item name="android:fadingEdge">none</item>
  9. <item name="android:scrollingCache">false</item>
  10. </style>
b, in a layout if there is a set of buttons to change the color when the corresponding is selected (this group of buttons is not an array), this can be defined as follows: Add a Selector_btn.xml file under the drawable file
  
 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. < selector xmlns : android = "http://schemas.android.com/apk/res/android"
  3. /span>< item android : state_enabled = "true" Android : drawable = "@drawable/frame_button_n" ></ item > <!-- under normal circumstances -->
  4. /span>< item android : state_enabled = "false" Android : drawable = "@drawable/frame_button_p" ></ item > <!-- button changes when pressed -->
  5. </selector>
In the reference can be directly used android:background= "@drawable/selector_btn", note the red part of the property, can not be written state_pressed this property, in order to prevent the interface repeatedly click button Events, So when you click the button, you can pass in
  
 
  1. articleManager.setOnClickListener(frameArticleBtnClick(articleManager, 1));
  2. /**
  3. * @param btn
  4. * @param flag
  5. * 记录哪个按钮被选中
  6. * @return 当按下相应的按钮时颜色发生变化,并且该按钮处于不可重复点击状态
  7. */
  8. Span class= "KWD" >private onclicklistener Framearticlebtnclick ( final button btn final int flag ) {
  9. return new OnClickListener() {
  10. @Override
  11. public void onClick(View v) {
  12. if (btn == articleManager) {
  13. articleManager.setEnabled(false);
  14. } else
  15. articleManager.setEnabled(true);
  16. if (btn == articleTypeManager) {
  17. articleTypeManager.setEnabled(false);
  18. } else
  19. articleTypeManager.setEnabled(true);
  20. currentFlagBtn = flag;
  21. }
  22. };
  23. }









From for notes (Wiz)

Change the color of the button when pressed in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.