Android 动态改变对话框值

Android <wbr>动态改变对话框值

  * 动态改变对话框值
  * 关键点:onPrepareDialog方法 里面调用 removeDialog(id);
  * 对话框调用执行顺序 showDialog() -> onCreateDialog() -> onPrepareDialog() -> removeDialog()
  * public final boolean showDialog(int id, Bundle args) {
              if (mManagedDialogs == null) {
                      mManagedDialogs = new SparseArray<ManagedDialog>();
              }
              ManagedDialog md = mManagedDialogs.get(id);
              if (md == null) {
                      md = new ManagedDialog();
                      md.mDialog = createDialog(id, null, args);
                      if (md.mDialog == null) {
                              return false;
                      }
                      mManagedDialogs.put(id, md);
              }
             
              md.mArgs = args;
              onPrepareDialog(id, md.mDialog, args);
              md.mDialog.show();
              return true;
    }
    分析showDialog方法可知
  android 创建的Dialog对象统一由ManagedDialogs进行管理,如果想重新执行createDialog()方法,需先将Dialog,移出ManagedDialogs;

public class DialogActivity extends Activity implements OnClickListener {
      private static final int DIALOG_SORT_MAILS = 0x1;
      public Button mButton;
      public boolean flag;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_demo);

            LinearLayout layout = (LinearLayout) findViewById(R.id.nextMail);
            mButton = new Button(this);
            mButton.setText("对话框1");
            layout.addView(mButton);
            mButton.setOnClickListener(this);
            setTitle("动态改变对话框值");
      }

      @Override
      public void onClick(View v) {
            showDialog(DIALOG_SORT_MAILS);
      }

      @Override
      public void onPrepareDialog(int id, Dialog dialog) {
            Log.d("tag", "onPrepareDialog ");
            switch (id) {
            case (DIALOG_SORT_MAILS):
                  Log.d("tag", "removeDialog ");
                  removeDialog(id);
                  break;
            }
      }
     
      @Override
      protected Dialog onCreateDialog(int id) {
            Log.d("tag", "onCreateDialog ");
            AlertDialog.Builder builder = new AlertDialog.Builder(
                        DialogActivity.this);
            switch (id) {

            case DIALOG_SORT_MAILS:
                  builder.setTitle("title");
                  if (flag) {
                        flag = false;
                        mButton.setText("对话框1");
                        builder.setSingleChoiceItems(R.array.dialog_demo1_entries, 0,
                                    new DialogInterface.OnClickListener() {
                                          public void onClick(DialogInterface dialog, int item) {

                                                dialog.cancel();
                                          }
                                    });
                  } else {
                        flag = true;
                        mButton.setText("对话框2");
                        builder.setSingleChoiceItems(
                                    R.array.dialog_demo2_entries, 0,
                                    new DialogInterface.OnClickListener() {
                                          public void onClick(DialogInterface dialog, int item) {

                                                dialog.cancel();
                                          }
                                    });
                  }
                  builder.setNegativeButton("cancel",
                              new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                          dialog.cancel();
                                    }
                              });
                  break;
            }
            AlertDialog alert = builder.create();
            return alert;

      }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值