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

Max*_*din 3 android textview android-alertdialog android-view

我想以编程方式在自定义对话框中包含的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>"));
        ...
Run Code Online (Sandbox Code Playgroud)

Min*_*tdh 10

试试这个:

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

  • 有点晚了,但你也可以使用 `TextView tv = myDialog.findViewById(R.id.myDialogsTextView)` (2认同)