標籤:blog xmlns wrapper one demo int length actor html
button(Button)是Android其中一個經常使用的UI組件。非常小可是在開發中最經常使用到。一般通過與監聽器結合使用。從而觸發一些特定事件。
Button繼承了TextView。它的功能就是提供一個button,這個button能夠供使用者點擊。當使用者對button進行操作的時候,觸發對應事件,如點擊。觸摸。一般對於一個button而言,用的最多的就是點擊事件,Button間接繼承自View。而Android UI中的全部事件。都是定義在View中的。
執行個體:ButtonDemo
執行效果:
代碼清單:
布局檔案: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源碼檔案: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知識點
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