在自定义对话框的TextView中设置文本

时间:2015-08-12 07:49:04

标签: android android-view textview alertdialog

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

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>"));
        ...

1 个答案:

答案 0 :(得分:9)

试试这个:

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