標籤:
a001.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btnShow" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:text="顯示多士" />12 13 </LinearLayout>
A001.java
1 package com.example.main; 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 A001 extends Activity {11 12 @Override13 protected void onCreate(Bundle savedInstanceState) {14 super.onCreate(savedInstanceState);15 setContentView(R.layout.a001);16 17 // 擷取 button 控制項18 Button button = (Button) findViewById(R.id.btnShow);19 20 //button 點擊事件21 button.setOnClickListener(new OnClickListener() {22 23 @Override24 public void onClick(View v) {25 //點擊 button 顯示多士26 Toast.makeText(A001.this, "按鈕控制項", Toast.LENGTH_SHORT).show();27 }28 });29 }30 }
Android——button控制項使用