Android Common UI Components-Button

Source: Internet
Author: User

button is one of the most common UI components in Android, very small but most commonly used in development. It is typically used in conjunction with a listener to trigger some specific events. Button inherits the TextView. Its function is to provide a button, this button can be clicked by the user, when the user action on the button, trigger the corresponding event, such as Click, Touch. In general, for a button, the most used is the Click event, the button indirectly inherits from view, and all the events in the Android UI are defined in the view.

Example: Buttondemo
operating effect:



Code Listing:
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent "    >    <button android:id=" @+id/button1 "        android:layout_width=" Fill_parent        " android:layout_height= "Wrap_content" android:text= "Button1"/>    <button android:id= "@+id/button2"        Android:layout_width= "Fill_parent"        android:layout_height= "wrap_content" android:text= "Button2"/></ Linearlayout>

Java source code file: Activitybutton.java
Package Com.rainsong.buttondemo;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.toast;public class    Activitybutton extends activity{Button btn1;        Button btn2;    Onclicklistener Listener1;    Onclicklistener Listener2; /** called when the activity is first created.        */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Listener1 = new Onclicklistener () {public void OnClick (View v) {toast.maketext (Activitybutton.            This, "Button1 clicked", Toast.length_short). Show ();        }        }; Listener2 = new Onclicklistener () {public void OnClick (View v) {toast.maketext (Activitybutton.            This, "Button2 clicked", Toast.length_short). Show ();        }        }; BTN1 = (Button) Findviewbyid (r.id. button1);        Btn1.setonclicklistener (Listener1);        BTN2 = (Button) Findviewbyid (R.id.button2);    Btn2.setonclicklistener (LISTENER2); }}

API Knowledge points
Activity

public class
Activity
Extends Contextthemewrapper
Implements COMPONENTCALLBACKS2 Keyevent.callback Layoutinflater.factory2 View.oncreatecontextmenulistener Window.callback

View Findviewbyid (int id)
Finds A view that is identified by the id attribute from the XML is processed in OnCreate (Bundle).

void Setcontentview (int layoutresid)
Set the activity content from a layout resource.

View
public class
View
Extends Object
Implements Drawable.callback Keyevent.callback Accessibilityeventsource

void Setonclicklistener (View.onclicklistener l)
Register a callback to being invoked when the this view is clicked.

Button
public class
Button
Extends TextView

View.onclicklistener
public static interface
View.onclicklistener

abstract void OnClick (View v)
Called when a view has been clicked.

Toast
public class
Toast
Extends Object

Constants
int Length_long showthe View or text notification for a LONG period of time.
int Length_short showthe View or text notification for a short period of time.

Static Toast
Maketext (context context, int resId, int duration)
Make a standard toast, the just contains a text view with the text from a resource.

Static Toast
Maketext (Context context, charsequence text, int duration)
Make a standard toast, that just contains a text view.

void
Show ()
Show The View for the specified duration.

Android Common UI Components-Button

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.