<?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>
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); } }
原文地址:http://blog.csdn.net/hantangsongming/article/details/40340025