Introduction to the use of buttons and Android listeners in the Android control series

Source: Internet
Author: User

Objective:

1. Learn how to create a Button in Android
2. Master common attributes of a Button
3. Master the Button click event (listener)

A Button is one of the most commonly used controls in various UIS. It is also one of the most popular controls in Android development. You can touch it to trigger a series of events, you need to know that a Button without clicking an event does not make any sense, because the user's fixed thinking is to see it and want to click it!

Let's take a look at the general Button in Android:

And the appearance after the Button in the dot:

I have emphasized the importance of layout and code separation in XML static resources of the Android control series. Therefore, the Button in this example will be implemented in this way:

1. To set the value on the Button (such as "OK"), we store the value as a resource in Res \ values \ strings. xml:

<String name = "btnText"> OK </string>

BtnText can be regarded as the key or ID of this value. If it is determined, it is its value. We finally bind it with its key. The Android system will automatically find its value based on the key.

2. In the XML layout file under the layout directory, add a Button

Copy codeThe Code is as follows: <Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/btnText"
> </Button>

Among them, the layout_width and layout_height attributes are required, but the specific value assignment can be based on your project needs

Generally, it can be a specific size, that is, number + unit, such as android: layout_height = "30px"

Or set the enumerated values:

Fill_parent is automatically scaled up to the same size as the parent control. For example, android: layout_width = "fill_parent" indicates that its width will fill the horizontal control of the parent control.

Wrap_content: it determines the size based on the control that occupies its own value. Generally, the control that uses this value will be smaller, and the advantage is that it does not need to measure the specific size, it will display all the values.

The Text attribute displays the button value. If android: layout_width uses the wrap_content attribute, the length of this value implicitly determines the button width.

In this example, we use the btnText value of the source file as the value of the Button. If we want to change "OK" to "cancel", we only need to change strings. the value in xml can be used without modifying any java code. It is also very useful for those who need to port the project to other language versions, such as selling the software to foreigners, you can change "OK" to "OK" without re-compiling.

3. Declare the ID for the Button

Add the ID attribute for the Button in XML

Android: id = "@ + id/btnOK"

+: It is used to generate static resources. If there is no +, it indicates that the static resources at the specified position are used. Generally, the + method is used when the control is assigned an ID.

After saving the XML file, you can find that R. java already has an internal class id. This class has a static field named btnOK. You don't have to worry about its specific value. We can get it in code.

4. Get the Button instance in Activity

If you want to back each control ID, it may be difficult. A more appropriate way is to use R. id to obtain the specified name, which uniquely corresponds to the Control ID.

Generally, if we want to use a button in an Activity, you should first think of findViewById. Through the static ID in R, we can easily obtain the control instance, this is about Android:

Button btn = (Button) findViewById (R. id. btnOK );

If the system does not recognize it, you need to import the class package of the Android Button:

Copy codeThe Code is as follows: import android. widget. Button;

You can find this Button at any time within the lifecycle of the Activity, but I strongly mind that if you need to call this Button multiple times, find it using findViewById in onCreate, record it in a global variable of Activity, and then do not need to find the Button later, because find itself also takes time, even if you are not aware of it, but it does take time. Because Android runs on a mobile phone or tablet, we recommend that you fully consider the simplicity and efficiency of the code during coding to save resources and power.

After obtaining the Button instance, we can use the code to assign values to it, such as btn. setText ("the Button value has changed ");

5. Customize the listener for the button:

A. What is A listener?

Remember the following:

1. The Listener is an abstract class that contains the function that the system will call when an event is triggered.

2. In the subclass, rewrite this function according to your project's needs.

3. The derived listener needs to be bound to a button, just like a headset that can make a sound, but you can't hear it without wearing it. In general, this button may need this listener, and another button needs another listener. each listener performs its duties. However, when its functions are similar, multiple buttons can also be bound to one listener.

4. Various controls have common events, such as clicking a button, dragging a scroll bar, and switching a ListView option, the function naming rule bound to the Listener is setOn *** Listener.

B. Why is this design?

1. When a user (or a system) triggers an event of a control, it often needs to handle some details, but they are not universal. For example, you may click a button to change its value, you may also want to click the button to bring up a webpage or click the button to close the current Activity. Because the results may be too diverse, designers simply leave the implementation to the Android openers for implementation.

2. The Listener is actually a callback:

It does not require you to execute the On *** Listener, but is automatically called after the system is triggered. The process is as follows:

C. How to customize listeners?

You can use custom internal classes to inherit listener abstract classes and implement abstract methods. You can also use the anonymous Implementation of the abstract class provided by Java:Copy codeThe Code is as follows: @ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button btn = (Button) findViewById (R. id. btnOK );
// Bind an anonymous listener and execute the logic code you want to execute after clicking the button
Btn. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Toast. makeText (MyActivity. this, "click the button", Toast. LENGTH_LONG). show ();
}
});
}

Summary:

This article describes how to use the Button method in Android, and focuses on the design purpose, running process, and usage of the event listener.

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.