设置TextView的TEX通过DialogFragment创建自定义对话框 [英] Set TextView tex on the custom dialog created through DialogFragment

查看:144
本文介绍了设置TextView的TEX通过DialogFragment创建自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个对话是这样的:

I'm creating a dialog like this:

public class TextReaderDialog extends DialogFragment {
    private View form = null;

    public void show(FragmentManager manager, String tag, String customText) {
        super.show(manager, tag);

        // ---- form is not created here yet, so I get NullPointerException
        TextView tv = (TextView) form.findViewById(R.id.transcription);
        tv.setText(customText);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        form = getActivity().getLayoutInflater().inflate(R.layout.text_reader_dialog, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.text_reader_dialog_title);
        builder.setView(form);
        return builder.create();
    }

和显示来自这样的活动吧:

And showing it from the activity like this:

new TextReaderDialog().show(getFragmentManager(), "tag-string", "String to show");

的问题是,在这里:

The problem is that here:

TextView tv = (TextView) form.findViewById(R.id.transcription);
tv.setText(customText);

形式尚不可用,并指向空。我怎么能显示来自活性的研究在对话框上传递的信息?

form is not yet available and points to null. How can I show the message passed from acitivity on the dialog?

推荐答案

您需要通过捆绑来传递你的论点。
然后您检索它的onCreate
你也可以在 onCreateDialog 使用它。

You need to pass your argument via a Bundle. Then you retrieve it in onCreate. And you can use it in onCreateDialog.

此外,你不需要为一个简单的文本自定义视图。您可以通过显示的文字 setMessage AlertDialog.Builder 类。

Also you don't need a custom view for a simple text. You can display the text using setMessage from the AlertDialog.Builder class.

您可以找到 DialogFragment文档中的例子。

public static class MyDialogFragment extends DialogFragment {
int mNum;

/**
 * Create a new instance of MyDialogFragment, providing "num"
 * as an argument.
 */
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;
}

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

这篇关于设置TextView的TEX通过DialogFragment创建自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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