Use of Android Selector

Source: Internet
Author: User

The use of powerful selector, usually we always in the code after the click button in the setting background, the operation is very troublesome, in fact, there is a more simple way to use, is selector;

The layout is as follows:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " >        <EditTextAndroid:layout_margin= "10DP"Android:layout_width= "Match_parent"Android:layout_height= "40DP"        />    <ButtonAndroid:id= "@+id/bt"android:layout_centerinparent= "true"Android:layout_width= "100DP"Android:layout_height= "Wrap_content"Android:background= "@drawable/bg_bt"Android:text= "Test" />        <ButtonAndroid:layout_margintop= "20DP"Android:layout_below= "@+id/bt"Android:id= "@+id/bt2"android:layout_centerinparent= "true"Android:layout_width= "100DP"Android:layout_height= "Wrap_content" android:background= "@drawable/bg_bt" (reference) Android:text= "Test 2" />    </Relativelayout>

The layout under Drawable is as follows:

Bg_bt.xml

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <!--<item android:state_pressed= "true" android:state_focused= "false" android:drawable= "@color/red"/> -    <Itemandroid:state_selected= "true"android:drawable= "@color/red" />//Indicates the selected state    <Itemandroid:drawable= "@color/green"/>//Default Status not selected</selector>

You just need a word in the code to get it done.

1 @Override2      Public voidOnClick (View v) {3         Switch(V.getid ()) {4          Caser.id.bt:5Bt.setselected (true);6            bt2.setselected (false); 7Intent Intent =NewIntent ( This, Btest.class);8 startactivity (intent);9              Break;Ten          CaseR.ID.BT2: OneBt.setselected (false); A             bt2.setselected (true);//This sentence is the key, indicating that the click button clicked on the selected state  -              Break; -  the         default: -              Break; -         } -}

The default picture:

After the click:

In fact, this method of using a flexible combination of the properties of the control is as follows:

bt.setpressed (pressed);

bt.setenabled (enabled);

Bt.setfocusable (focusable);

The above can be implemented in conjunction with the style of selector in a code can be modified to modify the style of the page control;

and attached to the selector of the different attributes of said description:

You can set the following trigger states:

Description in English:

Android:state_pressed

Boolean . "True" if this item should was used when the object was pressed (such as when a button is touched/clicked); "False" if this item should is used in the default, non-pressed state.

If true, displays the picture when it is clicked, and if False is not pressed, the default is displayed.

Android:state_focused

Boolean . "True" if this item should was used when the object was focused (such as when a button is highlighted using the trackball/d- PAD); "False" if this item should is used in the default, non-focused state.

True to show when the focus is obtained; False, the focus is not displayed by default.

android:state_selected

Boolean . "True" if this item should was used when the object was selected (such as when a tab is opened); "False" if this item should was used when the object was not selected.

True to display the picture when it is selected; False to display the picture when it is not selected.

Android:state_checkable

Boolean . "True" if this item should was used when the object is checkable; "False" if this item should was used when the object was not checkable. (Only useful if the object can transition between a checkable and non-checkable widgets.)

True to display the picture when the checkbox is available; False to display the checkbox when it is not available.

Android:state_checked

Boolean . "True" if this item should was used when the object is checked; "False" if it should was used when the object is un-checked.

True to display the picture when the checkbox is selected; False to display the checkbox when it is selected.

Android:state_enabled

Boolean . "True" if this item should was used when the object is enabled (capable of receiving touch/click events); "False" if it should was used when the object is disabled.

True to display the picture when the component is available for use, false to display the picture when the component is not available.

Android:state_window_focused

Boolean . "True" if this item should was used when the application window had focus (the application is in the foreground), "false" I F This item should was used when the application window does not has focus (for example, if the notification shade are pull Ed down or a dialog appears).

True to display the picture when this activity has the focus at the top; false to display the picture when it is not in the front.

Explanation of Pure Chinese:

android:state_pressed If it is true, displays the picture when it is clicked, and if False is not pressed, the default is displayed.

android:state_focused If it is true, gets the focus when it is displayed, or false does not get the focus display default.

Android:state_selected If True, displays the picture when it is selected, or False if it is not selected.

Android:state_checkable if the value is true, the picture is displayed when the checkbox is available and False when the checkbox is not available.

android:state_checked if the value is true, the picture is displayed when the checkbox is selected, False, and displayed when the checkbox is selected.

android:state_enabled if the value is true, the picture is displayed when the component can be used, false, when the component is not available.

android:state_window_focused if the value is true, the picture is displayed when this activity gets the focus, false, when it is not first displayed.

and selector of the collection of use there are many, follow-up will be published in succession:

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.