button is one of the most frequently used UI components of Android. Very small but most often used in development. Generally used in conjunction with the listener. Triggering some specific events.
Button inherits the TextView. Its function is to provide a button, which can be clicked by the user. When the user operates on the button, the corresponding event is triggered, such as a click. Touch. In general, for a button, the most used is the Click event, button indirectly inherit from view. And all the events in the Android UI. are defined in the view.
Example: Buttondemo
Execution 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 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 frequently uses UI components-Button