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
[HTML]View Plaincopy
- <? 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
[Java]View Plaincopy
- 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