将参数传递给 DialogFragment [英] passing argument to DialogFragment

查看:39
本文介绍了将参数传递给 DialogFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一些变量传递给 DialogFragment,以便我可以执行操作.Eclipse 建议我应该使用

I need to pass some variables to DialogFragment, so I can perform an action. Eclipse suggests that I should use

Fragment#setArguments(Bundle)

但是我不知道如何使用这个功能.我如何使用它来将变量传递给我的对话框?

But I don't know how to use this function. How can I use it to pass variables to my dialog?

推荐答案

Using newInstance

public static MyDialogFragment newInstance(int num) {
    MyDialogFragment f = new MyDialogFragment();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}

并像这样获得 Args

And get the Args like this

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNum = getArguments().getInt("num");
    ...
}

在此处查看完整示例
http://developer.android.com/reference/android/app/DialogFragment.html

这篇关于将参数传递给 DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆