如何使用showDialog(id)更新AlertDialog内容(How to update AlertDialog content using showDialog(id))

编程入门 行业动态 更新时间:2024-10-10 21:27:42
如何使用showDialog(id)更新AlertDialog内容(How to update AlertDialog content using showDialog(id))

我希望在我的应用程序中有一个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().

更多推荐

本文发布于:2023-07-30 14:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   内容   showDialog   AlertDialog   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!