Android 程式開發:(九)用戶界面 —— 9.3 註冊事件監聽器

當用戶與視圖views進行交互的時候,views也會觸發事件。舉個例子,當用戶點擊瞭一個按鈕,你需要為這個事件服務,隻有這樣,才能去執行某些適當的行為。如果想這麼做的話,你就需要為視圖views註冊監聽。

使用上一節中的例子,在activity中有兩個Button。我們可以使用一個匿名的類,為按鈕設置點擊事件。

[java]
package net.learn2develop.UIActivity; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
 
public class UIActivityActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        //—the two buttons are wired to the same event handler— 
        Button btn1 = (Button)findViewById(R.id.btn1); 
        btn1.setOnClickListener(btnListener); 
         
        Button btn2 = (Button)findViewById(R.id.btn2); 
        btn2.setOnClickListener(btnListener); 
    } 
     
    //—create an anonymous class to act as a button click listener— 
    private OnClickListener btnListener = new OnClickListener() 
    { 
        public void onClick(View v) 
        { 
            Toast.makeText(getBaseContext(), 
                    ((Button) v).getText() + " was clicked", 
                    Toast.LENGTH_LONG).show(); 
        } 
    }; 
     
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    { 
        switch (keyCode) 
        { 
            case KeyEvent.KEYCODE_DPAD_CENTER: 
                Toast.makeText(getBaseContext(), 
                        "Center was clicked", 
                        Toast.LENGTH_LONG).show(); 
                break;                 
            case KeyEvent.KEYCODE_DPAD_LEFT: 
                Toast.makeText(getBaseContext(), 
                        "Left arrow was clicked", 
                        Toast.LENGTH_LONG).show(); 
                break;                 
            case KeyEvent.KEYCODE_DPAD_RIGHT: 
                Toast.makeText(getBaseContext(), 
                        "Right arrow was clicked", 
                        Toast.LENGTH_LONG).show(); 
                break;                 
            case KeyEvent.KEYCODE_DPAD_UP: 
                Toast.makeText(getBaseContext(), 
                        "Up arrow was clicked", 
                        Toast.LENGTH_LONG).show(); 
                break; 
            case KeyEvent.KEYCODE_DPAD_DOWN: 
                Toast.makeText(getBaseContext(), 
                        "Down arrow was clicked", 
                        Toast.LENGTH_LONG).show(); 
                break;                 
        } 
        return false; 
    } 
 

如果你點擊瞭OK按鈕或Cancel按鈕,對應的消息就會出現在屏幕上,這也就說明瞭,適當的事件被出發瞭。


除瞭定義一個匿名類,也可以定一個匿名內部類去處理事件。下面的例子展示如何處理EditText的onFocusChange()方法。

[java] 
//—create an anonymous inner class to act as an onfocus listener— 
EditText txt1 = (EditText)findViewById(R.id.txt1); 
txt1.setOnFocusChangeListener(new View.OnFocusChangeListener() 

    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
        Toast.makeText(getBaseContext(), 
            ((EditText) v).getId() + " has focus – " + hasFocus, 
            Toast.LENGTH_LONG).show(); 
    } 
}); 
當EditText獲取焦點,屏幕上就會打印出一行消息。


也可以使用匿名內部類,為兩個Button設置監聽。

[java]
      //—the two buttons are wired to the same event handler— 
      Button btn1 = (Button)findViewById(R.id.btn1); 
      //btn1.setOnClickListener(btnListener); 
      btn1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
        //—do something— 
    } 
});         
 
      Button btn2 = (Button)findViewById(R.id.btn2); 
      //btn2.setOnClickListener(btnListener); 
      btn2.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
        //—do something— 
    } 
}); 

那麼,到底該使用哪種方法去為視圖設置監聽呢?當你需要使用一個監聽器去為多個視圖服務的時候,匿名類就很有用處瞭。如果你隻需要為單一的視圖設置監聽,那麼就使用匿名內部類吧。

發佈留言