Android 在通过DialogFragment创建的自定义对话框上设置TextView tex
我正在创建这样一个对话框:Android 在通过DialogFragment创建的自定义对话框上设置TextView tex,android,Android,我正在创建这样一个对话框: 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
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();
}
new TextReaderDialog().show(getFragmentManager(), "tag-string", "String to show");
并从以下活动中展示:
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();
}
new TextReaderDialog().show(getFragmentManager(), "tag-string", "String to show");
问题在于:
TextView tv = (TextView) form.findViewById(R.id.transcription);
tv.setText(customText);
表单尚不可用,并指向null。如何在对话框上显示从活动传递的消息?您需要通过
包传递参数。
然后在onCreate
中检索它。
您可以在onCreateDialog
中使用它
此外,对于简单的文本,您不需要自定义视图。您可以使用AlertDialog.Builder
类中的setMessage
显示文本
您可以在以下文档中找到示例:
您可以创建一个自定义对话框,并为其进行布局,然后将其充气。有了它,你可以控制布局中的一切,这比Android股票对话框提供的要多得多。我一直在使用这样一个自定义对话框,我真正需要调用来显示自定义消息的是setProgressMessage
方法
public class CustomProgressDialog extends Dialog {
private TextView text;
private String textProgress;
public CustomProgressDialog(Context _context, int resID) {
this(_context, _context.getString(resID));
}
public CustomProgressDialog(Context _context, String txt) {
super(_context);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (android.os.Build.VERSION.SDK_INT >= 11) {
this.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
}
textProgress = txt;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog_custom_progress_bar);
this.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
text = (TextView) findViewById(R.id.textViewProgress);
text.setText(textProgress);
}
@Override
public void show() {
super.show();
}
@Override
public void dismiss() {
super.dismiss();
}
public void setProgressMessage(String aText) {
if (aText != null) {
text.setText(aText);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.cancel();
Log.w("CustomDialog", "Dialog Cancelled");
}
}
确保在布局中提供对话框的宽度
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:gravity="center"
android:padding="0dp">
<!-- Add stuff you need in your dialog here -->
</RelativeLayout>
尝试将两行移到onCreateDialog()方法中。似乎在onCreateDialog()之前调用了show()方法。谢谢,我正在从activitynew TextReaderDialog().setArguments(args).show(getFragmentManager(),“sample”)
中添加类似的参数,谢谢您的帮助,我不使用自定义对话框,因为据我所知,commonware
不建议使用它们,原因如下:对话框的一个挑战是配置更改,尤其是屏幕旋转。如果他们将设备从纵向旋转到横向(反之亦然),则可能在更改后对话框应保留在屏幕上。然而,由于Android想要破坏并重新创建活动,这将对您的对话产生可怕的影响。这里还有一点:预碎片,Android有一个“托管对话”功能,它将试图帮助您实现这一点。然而,随着片段的引入,出现了DialogFragment,它处理配置更改过程。。。现在,如果您有一个自定义视图。。。这真的没关系,我不太明白你最后的评论