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

问题描述

我想以编程方式设置自定义对话框中包含的TextView中的文本,以便可以使用Html.fromHtml.我应该在哪个函数中调用setText?我已经尝试在onCreateDialog中执行此操作,但这实际上并不会更改文本.

I want to set the text in a TextView contained in a custom dialog programmatically, so that I can use Html.fromHtml. In what function should I call setText? I've tried doing it in onCreateDialog, but this does not actually change the text.

public class InfoDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.infodialog, null));

        TextView textView = (TextView) getActivity().findViewById(R.id.info);
        textView.setText(Html.fromHtml("<h1>Text has been correctly set</h1>"));
        ...

推荐答案

尝试一下:

View content =  inflater.inflate(R.layout.infodialog, null);
builder.setView(content);
TextView textView = (TextView) content.findViewById(R.id.info);

这篇关于在自定义对话框的TextView中设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:17