Introduction to the button of the Android control series and the use of the Android listener _android

Source: Internet
Author: User
Tags event listener
Learning objectives:

1. How to create a button in Android
2, grasp the common attributes of button
3. Master 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, where users can touch it to trigger a series of events, and it makes no sense to know a button without a click event, because the user's fixed thinking is to see it and want to go!

Look at the normal button in Android:

And the appearance of the button after the point:

I have emphasized the importance of layout and code separation in the Android control family of XML static resources, so the button in this example I will implement in this way:

1, in order to set the value on the button (such as "OK" above), we put this value first as a resource in the Res\values\strings.xml:

<string name= "Btntext" > OK </string>

Where btntext can be regarded as the key or ID of this value, OK is its value, we finally bound is its key, the Android system will automatically find its value according to the key.

2, in the layout directory in the XML layout file, we add a button

Copy Code code as follows:

<button
Android:layout_width= "Fill_parent"
Android:layout_height = "Wrap_content"
android:text= "@string/btntext"
></Button>


Where the Layout_width and Layout_height properties are required, but the specific assignment can be based on your project needs

Generally can be specific size, that is: number + units, such as Android:layout_height = "30px"

or set several values for the enumeration:

Fill_parent it will automatically enlarge to the same size as the parent control, such as android:layout_width= "fill_parent" means that its width fills the horizontal control of the parent control

Wrap_content it will determine the size of its own control to occupy the value, the general use of this value of the control will be smaller, the advantage is not to measure the specific size, it will be exactly the right to display all the values show

The Text property displays the value of the button, and if Android:layout_width uses the Wrap_content property, the length of the value implicitly determines the width of the button.

In this example, we use the value of the Btntext of the resource file as the value of the button, so if we want to change "OK" to "Cancel", Only need to change the value in Strings.xml, no need to change any Java code, for those who need to transplant the project to other language version is also very useful, such as to sell the software to foreigners, you can change "OK" to "OK", and do not need to recompile.

3, for button declaration ID

Continue to add ID attribute to button in XML

Android:id= "@+id/btnok"

+ indicates that the static resource is generated through it, and if there is no +, which means that the static resource is used at the specified location, the + method is used when the control is assigned an ID.

After you save the XML, you can see that there is already an internal class ID in the R.java, which has a static field called Btnok, whose exact value you don't care about, and we can get it in code.

4, in the activity to obtain this button instance

If you want to memorize each control ID, it may be difficult, and it is more appropriate to use R.id to get the specified name, which uniquely corresponds to the control ID

In general, if we want to use a button in an activity, the first way you think of it should be Findviewbyid, with the static ID in R, we can easily get the control instance, and as for how to find it, this is android:

Button btn = (button) Findviewbyid (R.id.btnok);

If the system is not recognized, you will need to import the package for the Android button:

Copy Code code as follows:

Import Android.widget.Button;


You can find this button at any time during the life of the activity, but I strongly mind that if you need to call this button multiple times, use Findviewbyid in OnCreate to find it, and then record it in a global variable of the activity. And then you don't need to find this button again, because found itself takes time, even if you're not aware of it, but it does take time. Because Android is running on a mobile phone or a tablet, I recommend that you encode your code to take into account the simplicity and efficiency of your coding to conserve resources and power.

After obtaining an instance of this button, we can use the code to assign a value to him, such as Btn.settext ("button value changed");

5. Customize the Listener for the button:

A, what is a listener?

You need to keep the following points in mind:

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

2, in the subclass, according to the needs of your project rewrite this function

3, the derived listener needs to be bound to the button, just like a headset can make a sound, but you do not wear it, you do not hear the sound of it. The general situation is that this button may need this listener, and another button requires a different listener, each listener each other, but when the function is similar, can also be multiple buttons together to bind a listener.

4, a variety of controls, there are commonly used events, such as the click of a button, drag a scroll bar, switch a listview option, and so on, his binding listener function naming rules are Seton****listener

B, why do you want to design this?

1, when the user (also possible system) triggers a control of an event, often to deal with some details, but they do not have versatility, such as you may click the button to change its value, you may want to click the button after the pop-up page, or click the button to close the current activity. Just because the results may be too diverse, designers simply leave the implementation entirely to the Android open to implement

2, the listener is actually a callback:

It does not require you to perform on****listener, but is automatically invoked when triggered by the system, as shown in the following diagram:


C, how to customize the listener?

You can use custom inner classes to inherit listener abstract classes and implement abstract methods. You can also use the anonymous implementation of Java-supplied abstract classes:
Copy Code code as follows:

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button btn = (button) Findviewbyid (R.id.btnok);
Bind anonymous listeners and execute the logical code that 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, "clicked the button", Toast.length_long). Show ();
}
});
}

Summarize:

This article describes how to use the button in Android, and focuses on the design purpose, running process, and usage of the event listener.
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.