安卓開發的監聽事件
阿新 • • 發佈:2019-02-11
為按鈕設定監聽事件分情況
1;
有多個按鈕,一個按鈕繫結一個事件。
import java.view.view.OnClickListener;
searchworld.setOnClickListener(new listener());
publicclass 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處理,這樣程式碼看起來簡潔了很多。