Android 目前主要有如下几种监听方式:
第一种:一个按钮对应一个监听。
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"); } });
第二种方式:多个按钮对应着一个监听。
Button start,stop; 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\n"); start_recorde(); break; case R.id.button2: Log.d(TAG, "Stop to recorder video\n"); 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>
对应的java代码如下:
Button btn = (Button) findViewById(R.id.button1); public void mybuttonlistener(View target){ //do something5 }
这里我就说完了监听的几种方式,下面我们再来看看监听实现的几种方式:
第一种:自身类作为事件监听器
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1 = (Button) findViewById(R.id.btn1); btn1.setText("单击"); btn1.setOnClickListener(this); } @Override public void onClick(View v) { Toast.makeText(this,"已单击我...",Toast.LENGTH_SHORT).show(); System.out.println("已单击我..."); } }
第二种方式:外部类作为事件监听器
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Act2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act2); Button btn2 = (Button) findViewById(R.id.btn2); btn2.setText("单击"); btn2.setOnClickListener(new OuterClass("已单击我...")); } } class OuterClass implements View.OnClickListener{ private String str="已单击我...."; public OuterClass(String str){ super(); this.str=str; } @Override public void onClick(View v) { System.out.println(str); } }
第三种方式:内部类作为事件监听器
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Act3 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act3); Button btn3 = (Button) findViewById(R.id.btn3); btn3.setText("单击"); btn3.setOnClickListener(new OuterClass()); } class OuterClass implements View.OnClickListener{ @Override public void onClick(View v) { Toast.makeText(Act3.this,"已单击我...",Toast.LENGTH_SHORT).show(); System.out.println("已单击我..."); } } }
第四种方式:匿名类作为事件监听器
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Act4 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act4); Button btn4 = (Button) findViewById(R.id.btn4); btn4.setText("单击"); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(Act4.this,"已单击我...",Toast.LENGTH_SHORT).show(); System.out.println("已单击我..."); } }); } }
以上我就说完了Android中按钮的点击监听事件。
欢迎start,欢迎评论,欢迎指正