1、一个Button控件对应一个监听 Button buttontest; buttontest = (Button) findViewById(R.id.button1); buttontest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("TEST", "button onClick"); } }); 2、多个button对应一个监听2: public class MainActivity extends Activity implements OnClickListener /*View.OnClickListener*/{ Button btn1,btn2,btn3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 =(Button) findViewById(R.id.btn1); btn2 =(Button) findViewById(R.id.btn2); btn3 =(Button) findViewById(R.id.btn3); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn1: btn1.setBackgroundResource(R.drawable.red); break; case R.id.btn2: btn2.setBackgroundResource(R.drawable.red); break; case R.id.btn3: btn3.setBackgroundResource(R.drawable.red); break; } } } 3、多个button对应一个监听1: start = (Button) findViewById(R.id.button1); stop = (Button) findViewById(R.id.button2); start.setOnClickListener(mylistener ); stop.setOnClickListener(mylistener ); View.OnClickListener mylistener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Log.d(TAG, "Start to recorder video "); start_recorde(); break; case R.id.button2: Log.d(TAG, "Stop to recorder video "); stop_recorde(); break; default: break; } } }; 四、xml中绑定监听 <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="mybuttonlistener"> </Button> Button btn = (Button) findViewById(R.id.button1); public void mybuttonlistener(View target){ //do something5 }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn1" android:textSize="32sp" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn2" android:textSize="32sp" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn3" android:textSize="32sp" /> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn4" android:textSize="32sp" /> </LinearLayout>
package com.gatsby.intentbundlesend; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btn1, btn2, btn3, btn4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } public void initView() { btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1: break; case R.id.btn2: break; case R.id.btn3: break; } } }