关于自定义对话框的问题

Questions regarding custom dialog

我在对话方面遇到了问题,所以我已经多次重新阅读 android 文档,但我仍然不确定以下内容,如果有人能回答我的问题,我将不胜感激... 在我问我的问题之前我会展示我的代码...

CustomDialog(直接从 android 开发网站复制)

public class FireMissilesDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        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.dialog_createlocation, null))
                .setTitle(R.string.dialog_createlocationtitle)
                // Add action buttons
                .setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        FireMissilesDialogFragment.this.getDialog().cancel();
                    }
                });
        return builder.create();
    }
}`

这是对话框的布局(dialog_createlocation.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
    android:id="@+id/EditTextName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="@string/name"
    android:maxLines="1"/>
<EditText
    android:id="@+id/EditTextAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp"
    android:fontFamily="sans-serif"
    android:hint="@string/address"
    android:maxLines="2"/>

问题:/n

  1. 在我的主activity中,我想从对话框中的两个EditText中获取文本。虽然我看到了一些关于这个的问题,但我太不知所措了,似乎无法理解答案。/n

2.Is 我有必要在自己的 class 中创建这个对话框吗?-我可以只在我的主 activity(- 中创建它而不创建内部 class)?/n

3.Im 对为什么要创建自定义对话框感到困惑,它必须扩展一个片段 - 为什么不只是一个 activity?/n

4.I 在我的主 activity 中创建上述对话框的一个实例(这不是片段),我在执行此操作时遇到了一些问题:

public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new FireMissilesDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");

}

谢谢!

  1. In my main activity, I want to get the text from the two EditText in the dialog. Although Ive seen some SO questions about this but im so overwelmed and cant seem to understand the answers.
EditText editTextName = dialog.getDialog().findViewById(R.id.EditTextName);
String name = editTextName.getText().toString();
  1. Is it necessary for me to create this dialog in its own class?-can i just create it in my main activity(- without creating an inner class)?

是的,可以。 AlertDialog 只是为您的对话框提供已经存在的结构。但是要自己制作,只需使用 Dialog Class.

3.Im confused with why to create a custom dialog, it has to extend a fragment-why not just an activity?

不必只使用Fragment for Dialog。根据第二个答案。

4.I create an instance of the above dialog in my main activity (which is not a fragment) and i got some issues doing this:

Post 堆栈跟踪或错误日志。