Android 0 Basics section 19th: Button usage explained

Source: Internet
Author: User
Tags gettext

The button is a very frequent component of Android development, primarily a button that is generated on the UI interface that the user can click and the button triggers an onclick Click event when the user clicks the button.

First, Button introduction

button is easy to use, you can specify the Android:background property for the button to increase the background color or background image, if the background image is set to irregular background image, you can develop a variety of irregular shape buttons.

If you use only normal background colors or background images, these backgrounds are fixed and do not change as the user moves. If you need to make the button's background color and background image change dynamically with the user action, you might consider using a custom drawable object, which is explained in detail in the Advanced Development section.

The sub-class that the button derives from is mainly a checkbox, RadioButton, ToggleButton, switch, and can be used to directly use the various properties and methods supported by the button, which will be learned later.

Second, button example

Next, a simple example program is used to learn the common use of button.

First, download two picture materials from the Internet, then put it in the res/drawable/directory, create a button_layout.xml file in the res/layout/directory, and populate it with the following code snippet:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <!--Normal Text button -    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal button"android:textsize= "16SP"Android:textcolor= "#ff00ff"/>    <!--Picture Button -    <ButtonAndroid:layout_width= "80DP"Android:layout_height= "80DP"Android:background= "@drawable/play"  />    <!--Picture button with text -    <ButtonAndroid:layout_width= "80DP"Android:layout_height= "80DP"Android:background= "@drawable/button"android:textsize= "18SP"Android:text= "Start"/>    </LinearLayout>

The first button in the upper interface layout is a normal button;

The second button is configured with the Background property, so the button will appear as a button for the shape of the background picture;

The third button combines text and background images, so the button appears as a button with text on the background image.

Then modify the App/src/java/mainactivity.java file to load the layout file as the new Button_layout.xml file. Running the program, you can see the interface effect shown.

With the above example, you know roughly how to create a button, then you'll continue to learn how to use the buttons and edittext in a comprehensive example.

Iii. Comprehensive Examples

Create a Login.xml file in the res/layout/directory and populate it with the following code snippet:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "User name:"android:textsize= "16SP"/>    <EditTextAndroid:id= "@+id/name_et"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter user name" />    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Password:"android:textsize= "16SP"/>    <EditTextAndroid:id= "@+id/pwd_et"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter password"Android:inputtype= "Textpassword"/>    <ButtonAndroid:id= "@+id/login_btn"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Login"/></LinearLayout>

Then modify the App/src/java/mainactivity.java file to load the layout file as the new Login.xml file. In order to listen for the Click event of the Login button, add the Click event Listener to the Java code as follows:

 Public classMainactivityextendsappcompatactivity {PrivateEditText Mnameet =NULL;//User name Input box    PrivateEditText Mpasswordet =NULL;//Password Entry box    PrivateButton mloginbtn =NULL;//Login Button@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);       Setcontentview (R.layout.login); //Get interface ComponentsMnameet =(EditText) Findviewbyid (R.id.name_et); Mpasswordet=(EditText) Findviewbyid (R.id.pwd_et); MLOGINBTN=(Button) Findviewbyid (R.ID.LOGIN_BTN); //bind a point-and-click event for the login buttonMloginbtn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {//gets the user name and password entered by the userString name =Mnameet.gettext (). toString (); String Password=Mpasswordet.gettext (). toString (); //message PromptToast.maketext (mainactivity. This,                        "Username:" + name + "\ n Password:" +password, toast.length_short). Show ();    }        }); }}

The above code uses an anonymous inner class to bind the login button to a point-and-click event Listener, and later learns other ways to bind the listener.

Run the program, enter the appropriate information in the User name input box and password input box, and then click the Login button, you can see the interface effect shown.

Here, the three most common components TextView, EditText, and buttons have been learned, have you mastered them?

------------------------------------------------

Come here today, the next issue begins the UI component learning. If you have questions welcome message together to explore, also welcome to join the Android 0 Basic introductory Technology discussion group, grow together!

Past period Summary share:

Android 0 Basics Introduction 1th: Android's past life

Android 0 Basics Section 2nd: Android system Architecture and application components those things

Android 0 Basics Section 3rd: Bring you up to talk about Android development environment

Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick

Android 0 Basics 5th: Use ADT bundles to easily meet the goddess

Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess

Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey

Android 0 Basics 8th: HelloWorld, the starting point for my first trip

Android 0 Basics 9th: Android app, no code can be developed

Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio

Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project

Android 0 Basics 12th: Get familiar with the Android studio interface and start selling

Android 0 Basics 13th: Android Studio Configuration optimization to create a development tool

Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era

Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing

Android 0 Basics Section 16th: Android User Interface Development overview

Android 0 Basics Section 17th: TextView Properties and Methods Daquan

Android 0 Basics Section 18th: EditText properties and how to use them

This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if reproduced please note source, hereby declare!

Android 0 Basics section 19th: Button usage explained

Related Article

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.