android初學筆記,android中處理button click事件的幾種寫法
阿新 • • 發佈:2019-02-12
介紹了給button設定click Listener的幾種寫法:
1:最簡單的是將處理該button點選事件的函式名字繫結到xml標籤上:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="handlerClick" />
然後在button所在的Activity裡面實現handlerClick()函式就可以了。
public void handlerClick(View v) {
Toast.makeText(this, "haha 你點了button1", Toast.LENGTH_LONG).show();
}
2:採用匿名內部類的方法,例項化OnClickListener()介面,作為形參傳給setOnClickListener()函式。
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "haha 你點了button2",
Toast.LENGTH_LONG).show();
}
});
這種寫法和下面的這種寫法的含義是一樣的。
button2.setOnClickListener(listener);
...若干句程式碼之後
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "haha 你點了button2" ,
Toast.LENGTH_LONG).show();
}
};
3:button所在的Activity實現OnClickListener介面,內部重寫OnClick函式。
public class MainActivity extends Activity implements OnClickListener{
...若干句程式碼之後
button3.setOnClickListener(this);
...若干句程式碼之後
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.button3) {
Toast.makeText(this, "haha 你點了 button3",Toast.LENGTH_LONG).show();
}
}
}
4: 採用內部類的實現方式
public class MainActivity extends Activity implements OnClickListener{
...若干句程式碼之後
button4.setOnClickListener(new MyClickListener());
...若干句程式碼之後
class MyClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "haha 你點了button4",
Toast.LENGTH_LONG).show();
}
}
}
整個Demo如下所示:
package com.example.buttontest;
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 MainActivity extends Activity implements OnClickListener {
Button button1;
Button button2;
Button button3;
Button button4;
public void initView() {
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button3 = (Button) this.findViewById(R.id.button3);
button4 = (Button) this.findViewById(R.id.button4);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
button2.setOnClickListener(listener);
button3.setOnClickListener(this);
button4.setOnClickListener(new MyClickListener());
}
public void handlerClick(View v) {
Toast.makeText(this, "haha 你點了button1", Toast.LENGTH_LONG).show();
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "haha 你點了button2",
Toast.LENGTH_LONG).show();
}
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.button3) {
Toast.makeText(this, "haha 你點了button3", Toast.LENGTH_LONG).show();
}
}
class MyClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "haha 你點了button4",
Toast.LENGTH_LONG).show();
}
}
}
佈局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="handlerClick"
android:text="繫結標籤的實現" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="匿名內部類的實現" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity作為監聽器" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="內部類的實現" />
</LinearLayout>