我希望在我的应用程序中有一个alertdialog,每次显示时都会更新其消息。 这是因为对话框值取决于应用程序上的某些值。
现在我尝试使用showDialog方法:
@Override public boolean onTouch(View arg0, MotionEvent arg1) { showDialog(RESULT_DIALOG); return false; }但是一旦创建了对话框,它就不会改变消息(我知道如果创建了对话框,它就会使用已启动的版本)。
我的onCreateDialog方法代码是:
public Dialog onCreateDialog(int dialogId) { AlertDialog dialog; switch(dialogId) { case RESULT_DIALOG: // do the work to define the pause Dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(localTv.getText()) .setCancelable(false) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); dialog = builder.create(); break; default: dialog = null; } return dialog; }有一种方法可以更新AlertDialog的内容。 实际上,每次调用onTouch事件时,我都会创建一个新的对话框。 但我不确定这是解决这个问题最干净的方法。
任何想法? 谢谢 :)
i want to have in my application an alertdialog, that has its message updated everytime it is showed. This is because the dialog box value depends on some values on the application.
Now i tried to use the showDialog method:
@Override public boolean onTouch(View arg0, MotionEvent arg1) { showDialog(RESULT_DIALOG); return false; }But once the dialog is created, it doesn't change the message (i know that if the dialog is created, it use the started version).
My onCreateDialog method code is:
public Dialog onCreateDialog(int dialogId) { AlertDialog dialog; switch(dialogId) { case RESULT_DIALOG: // do the work to define the pause Dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(localTv.getText()) .setCancelable(false) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); dialog = builder.create(); break; default: dialog = null; } return dialog; }There is a way to update the content of the AlertDialog. Actually i create a new dialog box every time the onTouch event is called. But i'm not sure that it is the cleanest way to solve that problem.
Any idea? Thanks :)
最满意答案
你必须使用onPrepareDialog方法:
@Override protected void onPrepareDialog ( int id, Dialog dialog ) { switch ( id ) { case RESULT_DIALOG: AlertDialog alertDialog = ( AlertDialog ) dialog; alertDialog.setMessage( localTv.getText() ); break; } super.onPrepareDialog( id, dialog ); }来自http://developer.android.com/guide/topics/ui/dialogs.html :
在显示对话框之前,Android还会调用onPrepareDialog(int,Dialog)上的可选回调方法。 如果要在每次打开时更改对话框的任何属性,请定义此方法。 每次打开对话框时都会调用此方法,而onCreateDialog(int)仅在第一次打开对话框时调用。 如果未定义onPrepareDialog(),则对话框将保持与上次打开时相同。 此方法还会传递对话框的ID以及您在onCreateDialog()中创建的Dialog对象。
You have to use onPrepareDialog method:
@Override protected void onPrepareDialog ( int id, Dialog dialog ) { switch ( id ) { case RESULT_DIALOG: AlertDialog alertDialog = ( AlertDialog ) dialog; alertDialog.setMessage( localTv.getText() ); break; } super.onPrepareDialog( id, dialog ); }From http://developer.android.com/guide/topics/ui/dialogs.html :
Before the dialog is displayed, Android also calls the optional callback method onPrepareDialog(int, Dialog). Define this method if you want to change any properties of the dialog each time it is opened. This method is called every time a dialog is opened, whereas onCreateDialog(int) is only called the very first time a dialog is opened. If you don't define onPrepareDialog(), then the dialog will remain the same as it was the previous time it was opened. This method is also passed the dialog's ID, along with the Dialog object you created in onCreateDialog().
更多推荐
发布评论