为按钮设置监听事件分情况
1;
有多个按钮,一个按钮绑定一个事件。
import java.view.view.OnClickListener;
searchworld.setOnClickListener(new listener());
public class listener implements OnClickListener{
public void onClick(View view)
{
String result=null;
String sql="select chinese from t_words where english=?";
Cursor cursor=database.rawQuery(sql, new String[]{word.getText().toString()});
if(cursor.getCount()>0)
{
cursor.moveToFirst();
result=cursor.getString(cursor.getColumnIndex("chinese")).replace("&", "&");
}
showResult.setText(word.getText()+"\n"+result.toString());
}
}
2.多个按钮,但是只用一个监听事件。
在android应用程序中,有时要用到很多的按钮元件,每个按钮都要有一个监听事件,为了让代码看起来干净简洁,并节省一些内存,我们可以用一个监听器(Listener)来实现多个按钮的onClick监听,下面是一个具体的例子:
- package com.android;
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class IntentSelectActivity extends Activity implements View.OnClickListener{
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button button1 = (Button)findViewById(R.id.btn1);
- Button button2 = (Button)findViewById(R.id.btn2);
- Button button3 = (Button)findViewById(R.id.btn3);
- button1.setOnClickListener(this);
- button1.setTag(1);
- button2.setOnClickListener(this);
- button2.setTag(2);
- button3.setOnClickListener(this);
- button3.setTag(3);
- }
- public void onClick(View v){
- int tag = (Integer) v.getTag();
- switch(tag){
- case 1:
- Intent music = new Intent(Intent.ACTION_GET_CONTENT);
- music.setType("audio/*");
- startActivity(Intent.createChooser(music, "Select music"));
- break;
- case 2:
- Intent dial = new Intent();
- dial.setAction("android.intent.action.CALL");
- dial.setData(Uri.parse("tel:13428720000"));
- startActivity(dial);
- break;
- case 3:
- Intent wallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
- startActivity(Intent.createChooser(wallpaper, "Select Wallpaper"));
- break;
- default :
- break;
- }
- }
- }
package com.android;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentSelectActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.btn1);
Button button2 = (Button)findViewById(R.id.btn2);
Button button3 = (Button)findViewById(R.id.btn3);
button1.setOnClickListener(this);
button1.setTag(1);
button2.setOnClickListener(this);
button2.setTag(2);
button3.setOnClickListener(this);
button3.setTag(3);
}
public void onClick(View v){
int tag = (Integer) v.getTag();
switch(tag){
case 1:
Intent music = new Intent(Intent.ACTION_GET_CONTENT);
music.setType("audio/*");
startActivity(Intent.createChooser(music, "Select music"));
break;
case 2:
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:13428720000"));
startActivity(dial);
break;
case 3:
Intent wallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(wallpaper, "Select Wallpaper"));
break;
default :
break;
}
}
}
这段代码用三个按钮实现了三个Intent意图:音乐播放、自动拨号、背景选择。只用了一个onClick处理,这样代码看起来简洁了很多。