Android learning notes-Button, android-button

Source: Internet
Author: User

Android learning notes-Button, android-button

Reference: http://www.runoob.com/w3cnote/android-tutorial-button-imagebutton.html

 

A Button is a subclass of TextView. Therefore, many attributes of TextView can be applied to the Button! In our actual development, the Button is nothing more than the corresponding operations on the Button status. For example, when the Button is pressed, a color is used to bring up another color, or a color when the button is unavailable! The above implementation is nothing more than throughStateListDrawableThis Drawable resource is used to compile a drawable resource file.

1. Introduction to StateListDrawable:

StateListDrawable is a type of Drawable resource. You can set different image effects and key nodes based on different statuses.<Selector>You only need to set the background attribute of the Button to the drawable resource. Different Button colors or backgrounds are displayed when you press the Button!

Attributes we can set:

 
 
  • Drawable: The referenced Drawable bitmap. We can put it at the beginning to indicate the normal state of the component ~
  • State_focused: Whether to obtain the focus
  • State_window_focused: Whether to obtain the 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. If a scroll wheel exists
  • State_pressed: Whether the control is pressed
  • State_active: Whether the control is active. For example, slidingTab
  • State_single: When the control contains multiple child controls, determine whether to display only one child Control
  • State_first: When the control contains multiple child controls, determine whether the first child control is displayed.
  • State_middle: When the control contains multiple child controls, determine whether a child control in the middle is displayed.
  • State_last: When the control contains multiple child controls, determine whether the last child control is displayed.
2. Push Button:

Well, let's look at the three image backgrounds first. We generally use .9.png as the button drawable to avoid pressing the button for stretch deformation! Let's take a look.Run:

Code implementation:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:drawable="@drawable/ic_course_bg_fen"/>    <item android:state_enabled="false" android:drawable="@drawable/ic_course_bg_pressed"/>    <item android:drawable="@drawable/ic_course_bg_cheng"/></selector>

Layout file:Activity_main.xml

<Button android: id = "@ + id/btnOne" android: layout_width = "match_parent" android: layout_height = "64dp" android: background = "@ drawable/btn_bg1" android: text = "Button"/> <Button android: id = "@ + id/btnTwo" android: layout_width = "match_parent" android: layout_height = "64dp" android: text = "button unavailable"/>

MainActivity. java:

Public class MainActivity extends Activity {private Button btnOne, btnTwo; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnOne = (Button) findViewById (R. id. btnOne); btnTwo = (Button) findViewById (R. id. btnTwo); btnTwo. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {if (btnTwo. getText (). toString (). equals ("button unavailable") {btnOne. setEnabled (false); btnTwo. setText ("button available");} else {btnOne. setEnabled (true); btnTwo. setText ("button unavailable ");}}});}}
3. Use the color value to draw the rounded corner button

In many cases, we may not have artists, right? Or we will not PS or destroy graph show, or we are lazy and don't want to make pictures by ourselves, at this time, we can write code as the button background and define the color of the button. Next we will customize the button background of the rounded corner ~, This involves another drawable Resource: ShapeDrawable, which is not detailed here. Next we will detail each drawable ~ It will be used here, But EditText only modifies the Background attribute. Only drawable resources will be pasted here!

Let's take a look at it first:

Bbuton_danger_rounded.xml:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"><shape>            <solid android:color="@color/bbutton_danger_pressed" />            <stroke android:width="1dp" android:color="@color/bbutton_danger_edge" />            <corners android:radius="@dimen/bbuton_rounded_corner_radius"/>        </shape></item>                <item android:state_enabled="false"><shape>        <solid android:color="@color/bbutton_danger_disabled" />            <stroke android:width="1dp" android:color="@color/bbutton_danger_disabled_edge" />            <corners android:radius="@dimen/bbuton_rounded_corner_radius"/>        </shape></item>            <item><shape>            <solid android:color="@color/bbutton_danger" />            <stroke android:width="1dp" android:color="@color/bbutton_danger_edge" />            <corners android:radius="@dimen/bbuton_rounded_corner_radius"/>        </shape></item>            </selector>

Color. xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="bbutton_danger_pressed">#ffd2322d</color>    <color name="bbutton_danger_edge">#ffd43f3a</color>    <color name="bbutton_danger_disabled">#a5d9534f</color>    <color name="bbutton_danger_disabled_edge">#a5d43f3a</color>    <color name="bbutton_danger">#ffd9534f</color>    <color name="text_font_white">#FFFFFF</color></resources>

Dimens. xml:

<dimen name="bbuton_rounded_corner_radius">5dp</dimen>

 

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.