The button control Usage example analysis of Android programming _android

Source: Internet
Author: User
Tags xml attribute

This example describes the button control usage of Android programming. Share to everyone for your reference, specific as follows:

One, button overview

Android.widget.Button directly inherits from Android.wdiget.TextView.

Direct subclasses are: Compoundbutton.

Indirect subclasses are: Checkbox,radiobutton,switch,togglebutton.

The Button class represents a buttons control. The button control can be pressed or clicked by the user to trigger another action.

Second, the Use of button

A typical usage:

public class MyActivity extends activity {
   protected void onCreate (Bundle icicle) {
     super.oncreate (icicle);
     Setcontentview (r.layout.content_layout_id);
     Final button button = (button) Findviewbyid (r.id.button_id);
     Button.setonclicklistener (New View.onclicklistener () {public
       void OnClick (View v) {
         //Perform action on click< c10/>}}
     );
   }

In addition to binding Onclicklistener for button in the activity class directly, you can bind triggered methods in an XML file by Android:onclick properties.

As an example:

<button 
   android:layout_height= "wrap_content" 
   android:layout_width= "Wrap_content" 
   @string/self_destruct " 
   android:onclick=" Selfdestruct "/>

Now, when the user presses this button, the system invokes the Selfdestruct (view) method in the activity, in order for this method to work, the method must be public and only one View parameter can be accepted. When the method is invoked, the clicked control is passed into the selfdestruct (view) method as a parameter of the view type. Such as:

public void selfdestruct (view view) { 
   //Kabloey 
} 

Three, XML attributes

The XML attribute of the button is basically consistent with the TextView. Interested friends can refer to the site related documents.

Four, common public methods

The common public method for button is basically consistent with TextView.

I hope this article will help you with the Android program.

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.