Android Basic Control Learning-----Button

Source: Internet
Author: User

Button Explanation:

First, in our actual use of button when the button is often different state will have a different display, in the explanation button before the drawable of the statelistdrawable below the relevant knowledge to speak about, Statelistdrawable in a drawable below a resource file, its key node selector, I just need to set the button property background @drawable/selector_name on it, At this point, depending on the state of the button changes now, of course, this statelistdrawable is also suitable for some other controls, mainly for the button.

Statelistdrawable The attributes we might use:

  • drawable: Referring to the drawable bitmap, we can put him to the front, it represents the normal state of the component ~
  • state_focused: whether to get focus
  • state_window_focused: Whether to get window focus
  • state_enabled: Whether the control is available
  • state_checkable: Can the control be checked, Eg:checkbox
  • state_checked: Whether the control is checked
  • state_selected: Whether the control is selected for the case of a scroll wheel
  • state_pressed: Whether the control is pressed
  • state_active: Whether the control is active, Eg:slidingtab
  • state_single: When a control contains multiple child controls, determines whether only one child control is displayed
  • State_first: When a control contains multiple child controls, determines whether the first child control is in the display state
  • state_middle: When a control contains multiple child controls, determines whether the middle one child control is in the display state
  • state_last: When a control contains multiple child controls, determines whether the last child control is in the display state

Two: Instance one: Realize the button pressing effect and the fillet of the button

Java files

 PackageCom.example.test3;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {PrivateButton btn1; PrivateButton btn2; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); BTN1=(Button) Findviewbyid (R.id.btn_one); BTN2=(Button) Findviewbyid (r.id.btn_two); Btn2.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {if(Btn2.gettext (). toString (). EndsWith ("button unavailable") {btn1.setenabled (false); Btn2.settext ("Button Available"); }Else{btn1.setenabled (true); Btn2.settext ("button is not available");    }            }        }); }}

Statelistdrawable file: Where the use of shape has been previously mentioned

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <!--Press the button -    <Itemandroid:state_pressed= "true">        <Shape>            <StrokeAndroid:width= "5px"Android:color= "@android: Color/holo_red_light"/>            <CornersAndroid:radius= "15DP"/>            <SolidAndroid:color= "@android: Color/holo_red_light"/>        </Shape>    </Item>    <!--button is not available -    <Itemandroid:state_enabled= "false">        <Shape>            <StrokeAndroid:width= "5px"Android:color= "@android: Color/darker_gray"/>            <CornersAndroid:radius= "15DP"/>            <SolidAndroid:color= "@android: Color/darker_gray"/>        </Shape>    </Item>    <!--Other conditions -    <Item>        <Shape>            <StrokeAndroid:width= "5px"Android:color= "@android: Color/holo_blue_bright"/>            <CornersAndroid:radius= "15DP"/>            <SolidAndroid:color= "@android: Color/holo_blue_bright"/>        </Shape>    </Item></selector>

Layout file:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background= "#ffffff">    <ButtonAndroid:id= "@+id/btn_one"Android:layout_width= "Match_parent"Android:layout_height= "70DP"Android:background= "@drawable/btn_bg1"Android:text= "button"Android:textstyle= "Bold"android:textsize= "24SP"/>    <ButtonAndroid:id= "@+id/btn_two"Android:layout_width= "Match_parent"Android:layout_height= "80DP"Android:layout_margintop= "10DP"Android:text= "button not available"android:textsize= "24SP"Android:textstyle= "Bold"        /></LinearLayout>

Android Basic Control Learning-----Button

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.