我试图将一些参数传递给 AlertDialog,这个对话框主要显示这两个参数(假设“foo”和“bar”参数),我正在使用 showDialog(int id)< 调用这个对话框
。 Activity 类中还有一个方法是通过一个Bundle
对象来传递参数:showDialog(int id, Bundle args)
,但是这个方法只适用于API 8 或更高版本我需要使用 API 7。
我在这里放置了一些代码块以简化我正在做的事情。
在我的 Activity 中,我像这样创建 AlertDialog:
protected Dialog onCreateDialog(int id) {
final LayoutInflater factory = LayoutInflater.from(this);
switch(id) {
case DIALOG_ID:
final View view = factory.inflate(R.layout.dialog_layout, null);
final TextView fooValue = (TextView)view.findViewById(R.id.foo_label);
final TextView barValue = (TextView)view.findViewById(R.id.foo_label);
//fooLabel.setText("HERE MUST BE FOO PARAMETER VALUE");
//barLabel.setText("HERE MUST BE BAR PARAMETER VALUE");
return new AlertDialog.Builder(this).
setIcon(R.drawable.icon).
setTitle(R.string.app_name).
setView(view).
setPositiveButton(R.string.close, null).
create();
...
在其他部分我称这个对话框为:
// THESE PARAMETERS MUST BE PASSED TO DIALOG
int foo = result.getInt("foo");
String bar = result.getString("bar");
showDialog(DIALOG_ID);
...
非常感谢!
最佳答案
我建议在实现上面的onCreateDialog
函数的类中添加一个方法setFooBar(int foo, String bar)
,以在之前接收foo 和bar 的值showDialog
被调用。
如果您没有 Activity 实例,请考虑将方法和变量设为静态。
关于android - 在 Android 中将参数传递给 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4764711/