2015-05-11 11:44:38 1515瀏覽
在Android中到處可見接口回調(diào)機制,尤其是UI事件處理方面。舉一個最常見的例子button點擊事件,button有一個點擊方法onClick(),我們知道onclick()是一個回調(diào)方法,當用戶點擊button就執(zhí)行這個方法。在源碼中是這樣定義的:
1.
//這個是View的一個回調(diào)接口 2. /** 3. * Interface definition for a callback to be invoked when a view is clicked. 4. */ 5. public interface OnClickListener { 6. /** 7. * Called when a view has been clicked. 8. * 9. * @param v The view that was clicked. 10. */ 11. void onClick(View v); 12. } 下面看一個簡單的例子: 1. import android.app.Activity; 2. import android.os.Bundle; 3. import android.view.View; 4. import android.view.View.OnClickListener; 5. import android.widget.Button; 6. import android.widget.Toast; 7. 8. public class MainActivity extends Activity implements OnClickListener{ 9. 10. private Button button; 11. 12. @Override 13. public void onCreate(Bundle savedInstanceState) { 14. super.onCreate(savedInstanceState); 15. setContentView(R.layout.activity_main); 16. button = (Button)findViewById(R.id.button1); 17. 18. 19. button.setOnClickListener(this); 20. } 21. 22. @Override 23. public void onClick(View v) { 24. Toast.makeText(getApplication(), "OnClick", Toast.LENGTH_LONG).show(); 25. } 26. 27. } 這就是一個很典型的例子,當然也可以這樣寫: 29. import android.app.Activity; 30. import android.os.Bundle; 31. import android.view.View; 32. import android.view.View.OnClickListener; 33. import android.widget.Button; 34. 35. public class SSSS extends Activity { 36. 37. private Button button; 38. private OnClickListener clickListener = new OnClickListener() { 39. 40. @Override 41. public void onClick(View v) { 42. // TODO Auto-generated method stub 43. 44. } 45. }; 46. 47. @Override 48. protected void onCreate(Bundle savedInstanceState) { 49. // TODO Auto-generated method stub 50. super.onCreate(savedInstanceState); 51. button = (Button)findViewById(R.id.button1); 52. button.setOnClickListener(clickListener); 53. } 54. 55. }
下面是View類的setOnClickListener方法,把和回調(diào)相關(guān)代碼貼出來。什么貼它呢,因為Button繼承于TextView,而TextView繼承于View,在View里面處理的回調(diào):
1.
public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource { 2. /** 3. * Listener used to dispatch click events. 4. * This field should be made private, so it is hidden from the SDK. 5. * {@hide} 6. */ 7. protected OnClickListener mOnClickListener; 8. 9. /** 10. * 11. * Register a callback to be invoked when this view is clicked. If this view is not 12. * clickable, it becomes clickable. 13. * 14. * @param l The callback that will run 15. * 16. * @see #setClickable(boolean) 17. */ 18. 19. public void setOnClickListener(OnClickListener l) { 20. if (!isClickable()) { 21. setClickable(true); 22. } 23. mOnClickListener = l; 24. } 25. 26. 27. /** 28. * Call this view's OnClickListener, if it is defined. 29. * 30. * @return True there was an assigned OnClickListener that was called, false 31. * otherwise is returned. 32. */ 33. public boolean performClick() { 34. sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); 35. 36. if (mOnClickListener != null) { 37. playSoundEffect(SoundEffectConstants.CLICK); 38. 39. mOnClickListener.onClick(this); 40. return true; 41. } 42. 43. return false; 44. } 45. }
那現(xiàn)在一起來總結(jié)一下基本的回調(diào)是如何實現(xiàn)的,首先創(chuàng)建一個接口,這個接口用于你在某個情景下執(zhí)行相應的操作。接著創(chuàng)建一個功能類,比如這個類可以顯示一個對話框、可以滑動菜單、可以下載數(shù)據(jù)等等。然后,在這個類里面聲明回調(diào)接口的對象,之后在這個類里面創(chuàng)建在某個情景下需要執(zhí)行的方法,而且在這個方法里面為聲明的接口對象賦值。最后在其他的類中使用這個功能類就可以了。所以說,最少也是需要三個類共同來完成這個回調(diào)機制。
這下大家應該就比較明白了,那我們就自己按照這個方式和流程完成一個這樣的例子。以Dialog為例,一般我們在開發(fā)時候,經(jīng)常會用到Dialog。比如一個彈出框,里面有確認和取消。通常情況下,我們可能會這樣寫:
1.
final Dialog dialog = new Dialog(this, R.style.MyDialogStyle);
2. dialog.setContentView(R.layout.dialog_exit_train);
3. dialog.show();
4. ImageButton ib_affirm = (ImageButton) dialog
5. .findViewById(R.id.dialog_exit_ib_affirm);
6. ImageButton ib_cancel = (ImageButton) dialog
7. .findViewById(R.id.dialog_exit_ib_cancel);
8.
9. ib_affirm.setOnClickListener(new View.OnClickListener() {
10.
11. @Override
12. public void onClick(View v) {
13.
14. saveUserData();
15. dialog.dismiss();
16. TestActivity.this.finish();
17. }
18. });
19.
20. ib_cancel.setOnClickListener(new View.OnClickListener() {
21.
22. @Override
23. public void onClick(View v) {
24.
25. dialog.dismiss();
26. }
[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
31. import android.app.Dialog;
32. import android.content.Context;
33. import android.os.Bundle;
34. import android.text.TextPaint;
35. import android.view.View;
36. import android.view.Window;
37. import android.view.WindowManager;
38. import android.widget.Button;
39. import android.widget.TextView;
40.
41. import com.fanfou.app.opensource.R;
42.
43. /**
44. *
45. *
46. */
47. public class AlertInfoDialog extends Dialog implements View.OnClickListener {
48. //創(chuàng)建接口
49. public static interface OnOKClickListener {
50. public void onOKClick();
51. }
52.
53. private final Context mContext;
54. private TextView mTitleView;
55. private TextView mTextView;
56.
57. private Button mButtonOk;
58. private CharSequence mTitle;
59.
60. private CharSequence mText;
61. //生命接口對象
62. private OnOKClickListener mClickListener;
63.
64. public AlertInfoDialog(final Context context, final String title,
65. final String text) {
66. super(context, R.style.Dialog);
67. this.mContext = context;
68. this.mTitle = title;
69. this.mText = text;
70. }
71.
72. private void init() {
73. setContentView(R.layout.dialog_alert);
74.
75. this.mTitleView = (TextView) findViewById(R.id.title);
76. final TextPaint tp = this.mTitleView.getPaint();
77. tp.setFakeBoldText(true);
78. this.mTitleView.setText(this.mTitle);
79.
80. this.mTextView = (TextView) findViewById(R.id.text);
81. this.mTextView.setText(this.mText);
82.
83. this.mButtonOk = (Button) findViewById(R.id.button_ok);
84. this.mButtonOk.setOnClickListener(this);
85.
86. }
27. });
28.
29.