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

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <button android:id="@+id/button1"
  8. android:layout_width="fill_parent"
  9. android:layout_height= "wrap_content" android:text="Button1" />
  10. <button android:id="@+id/button2"
  11. android:layout_width="fill_parent"
  12. android:layout_height= "wrap_content" android:text="Button2" />
  13. </linearlayout>


Java source code file: Activitybutton.java

[Java]View Plaincopy
  1. Package Com.rainsong.buttondemo;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.view.View;
  5. Import Android.view.View.OnClickListener;
  6. Import Android.widget.Button;
  7. Import Android.widget.Toast;
  8. Public class Activitybutton extends Activity
  9. {
  10. Button btn1;
  11. Button btn2;
  12. Onclicklistener Listener1;
  13. Onclicklistener Listener2;
  14. /** Called when the activity is first created. * /
  15. @Override
  16. public void OnCreate (Bundle savedinstancestate)
  17. {
  18. super.oncreate (savedinstancestate);
  19. Setcontentview (R.layout.main);
  20. Listener1 = New Onclicklistener () {
  21. public void OnClick (View v) {
  22. Toast.maketext (Activitybutton.   This, "Button1 clicked", Toast.length_short). Show ();
  23. }
  24. };
  25. Listener2 = New Onclicklistener () {
  26. public void OnClick (View v) {
  27. Toast.maketext (Activitybutton.   This, "Button2 clicked", Toast.length_short). Show ();
  28. }
  29. };
  30. BTN1 = (Button) Findviewbyid (R.id.button1);
  31. Btn1.setonclicklistener (Listener1);
  32. BTN2 = (Button) Findviewbyid (R.id.button2);
  33. Btn2.setonclicklistener (LISTENER2);
  34. }
  35. }


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.