1. 程式人生 > >安卓開發的監聽事件

安卓開發的監聽事件

為按鈕設定監聽事件分情況

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監聽,下面是一個具體的例子:

[java] view plaincopyprint?
  1. package com.android;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.net.Uri;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. public class IntentSelectActivity extends Activity implements View.OnClickListener{  
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         Button button1 = (Button)findViewById(R.id.btn1);  
  15.         Button button2 = (Button)findViewById(R.id.btn2);  
  16.         Button button3 = (Button)findViewById(R.id.btn3);  
  17.         button1.setOnClickListener(this);  
  18.         button1.setTag(1);  
  19.         button2.setOnClickListener(this);  
  20.         button2.setTag(2);  
  21.         button3.setOnClickListener(this);  
  22.         button3.setTag(3);  
  23.     }  
  24.     public void onClick(View v){  
  25.         int tag = (Integer) v.getTag();  
  26.         switch(tag){  
  27.         case 1:  
  28.             Intent music = new Intent(Intent.ACTION_GET_CONTENT);  
  29.             music.setType("audio/*");  
  30.             startActivity(Intent.createChooser(music, "Select music"));  
  31.             break;  
  32.         case 2:  
  33.             Intent dial = new Intent();  
  34.             dial.setAction("android.intent.action.CALL");  
  35.             dial.setData(Uri.parse("tel:13428720000"));  
  36.             startActivity(dial);  
  37.             break;  
  38.         case 3:  
  39.             Intent wallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);  
  40.             startActivity(Intent.createChooser(wallpaper, "Select Wallpaper"));  
  41.             break;  
  42.         default :  
  43.             break;  
  44.         }  
  45.     }  
  46. }  
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處理,這樣程式碼看起來簡潔了很多。