0
点赞
收藏
分享

微信扫一扫

Android 动态改变对话框值



 
 

   
 * 动态改变对话框值 
 
   
 * 关键点: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; 
 

   
   
    
 } 
 

 }

举报

相关推荐

Android:<9>对话框练习

0 条评论