main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id = "@+id/mcLayOut" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id = "@+id/mcTestBtn" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "请点击"/> <Button android:id = "@+id/mcTestABtn" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "外部点击A"/> <Button android:id = "@+id/mcTestBBtn" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "外部点击B"/> </LinearLayout> ClickEventHdlrActv .java package com.ClickEventHdlr; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; public class ClickEventHdlrActv extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //布局响应点击事件 LinearLayout layOut = (LinearLayout)findViewById(R.id.mcLayOut); layOut.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(ClickEventHdlrActv.this,"布局被点击",Toast.LENGTH_SHORT).show(); } }); //测试按钮监听按下事件(事件处理直接在设置方法得地方来定义) Button TestBtn = (Button)findViewById(R.id.mcTestBtn); TestBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(ClickEventHdlrActv.this,"按钮被按下",Toast.LENGTH_SHORT).show(); //Toast.makeText(KeyHdlrActv.this,"A按钮被按下",Toast.LENGTH_LONG).show(); } }); //测试按钮监听按下事件(事件单独定义类,并且,一个监听对象负责对多个对象事件的监听) m_TestABtn = (Button)findViewById(R.id.mcTestABtn); m_TestBBtn = (Button)findViewById(R.id.mcTestBBtn); OnBtnClickListener onBtnClickListener = new OnBtnClickListener(); m_TestABtn.setOnClickListener(onBtnClickListener); m_TestBBtn.setOnClickListener(onBtnClickListener); } private class OnBtnClickListener implements View.OnClickListener { @Override public void onClick(View v) { if (v.equals(m_TestABtn)){ Toast.makeText(ClickEventHdlrActv.this,"外部点击A被按下",Toast.LENGTH_SHORT).show(); }else if (v.equals(m_TestBBtn)){ Toast.makeText(ClickEventHdlrActv.this,"外部点击B被按下",Toast.LENGTH_SHORT).show(); } } } private Button m_TestABtn; private Button m_TestBBtn; }