Android(监听+回调=观察者)模式从Dialog到Activity传递数据

来源:互联网 发布:淘宝一淘是什么意思 编辑:程序博客网 时间:2024/05/08 04:40

一。自定义Dialog

  1. <LinearLayout>
  2. <EditText
  3. android:id="@+id/et"
  4. android:hint="文本" />
  5. <LinearLayout>
  6. <Button
  7. android:id="@+id/btn_no"
  8. android:text="取消" />
  9. <Button
  10. android:id="@+id/btn_ok"
  11. android:text="确定" />
  12. </LinearLayout>
  13. </LinearLayout>

二。自定义Dialog实现类,实现监听器方法,

  1. /***
  2. * 自定义的Dialog类,不是AlertDialog
  3. * 实现了View的点击监听
  4. * @author www.cuiweiyou.com
  5. */
  6. public class CustomAlertDialog extends android.app.Dialog implements android.view.View.OnClickListener {
  7. private View view;
  8. private Button btn_no;
  9. private Button btn_ok;
  10. private EditText et;
  11. /**
  12. * 一。内部接口,要求实现一个callback方法
  13. * @author Administrator cuiweiyou.com
  14. */
  15. private ICallBack icb;
  16.  
  17. /**
  18. * 初始化界面
  19. */
  20. private void initView (){
  21. view = LayoutInflater.from(getContext()).inflate(R.layout.layout_custom_alertdialog, null);
  22. et = (EditText) view.findViewById(R.id.et);
  23. btn_no = (Button) view.findViewById(R.id.btn_no);
  24. btn_no.setOnClickListener(this);
  25. btn_ok = (Button) view.findViewById(R.id.btn_ok);
  26. btn_ok.setOnClickListener(this);
  27. setContentView(view);
  28. }
  29.  
  30. protected CustomAlertDialog(Context context) {
  31. super(context);
  32. initView ();
  33. }
  34. public CustomAlertDialog(Context context, boolean cancelable,
  35. OnCancelListener cancelListener) {
  36. super(context, cancelable, cancelListener);
  37. initView ();
  38. }
  39.  
  40. public CustomAlertDialog(Context context, int theme) {
  41. super(context, theme);
  42. initView ();
  43. }
  44.  
  45. /**
  46. * 二。自定义的构造方法,需要一个ICallBack接口
  47. * @param context
  48. * @param icb 回调器
  49. */
  50. protected CustomAlertDialog(Context context, ICallBack icb) {
  51. super(context);
  52. this.icb = icb;
  53. initView ();
  54. }
  55. /**
  56. * 三。设置回调变量
  57. * @param cb
  58. */
  59. // 如果不是使用上面接收icallback参数的(二)构造方法创建此自定义对话框,则需要使用此方法指定icb变量
  60. public void setCallBack (ICallBack cb) {
  61. icb = cb;
  62. }
  63.  
  64. /**
  65. * 四。实现的监听器的方法,判断点击的控件
  66. * 当此自定义对话框点击按钮2时,向属性icb实例传一个参数
  67. * 这个参数会被界面接收使用
  68. */
  69. @Override
  70. public void onClick(View v) {
  71. switch (v.getId()) {
  72. case R.id.btn_no:
  73. dismiss();
  74. break;
  75. case R.id.btn_ok:
  76. if(icb != null){
  77. // 这个方法在传入的回调器中实现
  78. icb.callback(et.getText().toString());
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. /**
  86. * 一。内部接口,要求实现一个callback方法
  87. * @author Administrator cuiweiyou.com
  88. */
  89. interface ICallBack {
  90. void callback(String str);
  91. }
  92. }

三。自定义Dialog与Activity

  1. public class MainActivity extends Activity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. final TextView v = (TextView)findViewById(R.id.tv);
  8. /**
  9. * 使用自定义构造方法创建对话框
  10. * 第二个参数实现回调
  11. * 当点击对话框按钮时,在监听处理中调用回调方法,
  12. * 即现在实现的,传入的callback方法
  13. * 此方法接收 自定义对话框传入的文本框字串 ,设置到界面上
  14. */
  15. CustomAlertDialog dialog = new CustomAlertDialog(MainActivity.this, new ICallBack() {
  16. /** 通过自定义对话框所属的回调,传递参数到界面 **/
  17. @Override
  18. public void callback(String str) {
  19. v.setText(str);
  20. }
  21. });
  22. /*
  23. // 如果不是通过上面传入两个参数创建对话框,则须如此手动设置回调
  24. dialog.setCallBack(new ICallBack() {
  25. @Override
  26. public void callback(String str) {
  27. v.setText(str);
  28. }
  29. });
  30. */
  31. dialog.show();
  32. }
  33. }

下面是一点须要回复才能看到的东东:
【抱歉,只有对本文发表过评论才能阅读隐藏内容。】

0 0