Android常用UI組件 - Button

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   os   ar   使用   java   

按鈕(Button)是Android當中一個常用的UI組件,很小但是在開發中最常用到。一般通過與監聽器結合使用,從而觸發一些特定事件。 Button繼承了TextView。它的功能就是提供一個按鈕,這個按鈕可以供使用者點擊,當使用者對按鈕進行操作的時候,觸發相應事件,如點擊,觸摸。一 般對於一個按鈕而言,用的最多的就是點擊事件,Button間接繼承自View,而Android UI中的所有事件,都是定義在View中的。

執行個體:ButtonDemo
運行效果:



代碼清單:
布局檔案: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原始碼檔案:ActivityButton.java

[java] view plaincopy
  1. package com.rainsong.buttondemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.Toast;  
  9.   
  10. public class ActivityButton extends Activity  
  11. {  
  12.     Button btn1;  
  13.     Button btn2;  
  14.       
  15.     OnClickListener listener1;  
  16.     OnClickListener listener2;  
  17.   
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState)  
  21.     {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.   
  25.         listener1 = new OnClickListener() {  
  26.             public void onClick(View v) {  
  27.                 Toast.makeText(ActivityButton.this, "Button1 clicked",Toast.LENGTH_SHORT).show();  
  28.             }  
  29.         };  
  30.         listener2 = new OnClickListener() {  
  31.             public void onClick(View v) {  
  32.                 Toast.makeText(ActivityButton.this, "Button2 clicked",Toast.LENGTH_SHORT).show();  
  33.             }  
  34.         };  
  35.   
  36.         btn1 = (Button)findViewById(R.id.button1);  
  37.         btn1.setOnClickListener(listener1);  
  38.   
  39.         btn2 = (Button)findViewById(R.id.button2);  
  40.         btn2.setOnClickListener(listener2);  
  41.     }  
  42. }  


API知識點
Activity

public class
Activity
extends ContextThemeWrapper
implements ComponentCallbacks2 KeyEvent.Callback LayoutInflater.Factory2 View.OnCreateContextMenuListener Window.Callback

View     findViewById(int id)
Finds a view that was identified by the id attribute from the XML that was 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 be invoked when 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 that 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常用UI組件 - Button

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.