传递数据从活动到对话框 [英] Pass Data From Activity To Dialog
问题描述
我要寻找一种方式来从活动中传递数据到对话框。我想打电话给的ShowDialog(INT);
,但我不明白的方式对任何数据传递给对话框。
我需要一个字符串传递到对话框,以显示一个确认:)
I am looking for a way to pass data from an activity onto a dialog box. I am trying to call showDialog(int);
, however i don't see a way to pass any data to the dialog box.
I need to pass a string to the dialog box in order to display a confirmation :)
干杯
推荐答案
如果您的目标的Android 2.2(API级别8或更高版本),可以使用
If you are targeting Android 2.2 (API Level 8 or higher) you can use
public final boolean showDialog (int id, Bundle args)
和传递您的论点捆绑
。请参阅<一href="http://developer.android.com/reference/android/app/Activity.html#on$p$ppareDialog%28int,%20android.app.Dialog%29">documentation.
And pass your arguments in Bundle
. See documentation.
如果你想支持旧的Android版本,你要救你的论点活动
类成员,然后从你的<一个访问它们href="http://developer.android.com/reference/android/app/Activity.html#on$p$ppareDialog%28int,%20android.app.Dialog,%20android.os.Bundle%29"><$c$c>on$p$ppareDialog$c$c>功能。注意,<一href="http://developer.android.com/reference/android/app/Activity.html#onCreateDialog%28int%29"><$c$c>onCreateDialog$c$c>因为它为对话创造只调用一次不适合你的需求。
If you want to support older Android versions, you should save your arguments in Activity
class members and then access them from your onPrepareDialog
function. Note that onCreateDialog
won't fit your needs as it's called only once for dialog creation.
class MyActivity {
private static int MY_DLG = 1;
private String m_dlgMsg;
private showMyDialog(String msg){
m_dlgMsg = msg;
showDialog(MY_DLG);
}
private doSomething() {
...
showMyDlg("some text");
}
protected void onCreateDialog(int id){
if(id == MY_DLG){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
....
return builder.create();
}
return super.onCreateDialog(id);
}
@Override
protected void onPrepareDialog (int id, Dialog dialog){
if(id == MY_DLG){
AlertDialog adlg = (AlertDialog)dialog;
adlg.setMessage(m_dlgMsg);
} else {
super.onPrepareDialog(id, dialog);
}
}
}
这篇关于传递数据从活动到对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!