安卓開發的監聽事件

爲按鈕設置監聽事件分情況

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

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章