我想在自定义对话框中的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>"));
...
Activity.findViewById()
只会在传递给活动的setContentView()
方法的布局中查找,而在我的情况下,它并没有包括对话框使用的视图。 - Max RadinTextView tv = myDialog.findViewById(R.id.myDialogsTextView)
。 - Smitty-Werben-Jager-Manjenson