我如何创建自定义 AlertDialog,它能够将输入的数据传回 Android 中的调用 Activity?

How do I create a custom AlertDialog that will be able to pass inputted data back to the calling Activity in Android?

在我的应用程序中,我希望用户能够做三件事:

  1. 打开一个对话框。
  2. 允许用户在该对话框中输入两个字符串。
  3. 单击 "Okay" 按钮,将两个字符串传回调用 activity。

我正在尝试效仿 Here,但进展并不顺利。

这是我的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Some Text Here"
        android:textSize="18sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <TextView
            style="@style/my_custom_style"
            android:text="From: "
            android:labelFor="@+id/from"/>

        <EditText
            android:id="@+id/from"
            style="@style/my_custom_style"
            android:inputType="date"/>

        <TextView
            style="@style/my_custom_style"
            android:text="To: "
            android:labelFor="@+id/to"/>

        <EditText
            android:id="@+id/to"
            style="@style/my_custom_style"
            android:inputType="date"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="end"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel"/>

        <Button

            android:id="@+id/button_okay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/okay"/>

    </LinearLayout>

</LinearLayout>

这是我的 class 扩展 DialogFragment:

public class MyCustomDialogFragment extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
        alertDialogBuilder.setView(layoutInflater.inflate(R.layout.my_custom_layout, null));
        return alertDialogBuilder.create();
    }

}

现在,在我的调用中 activity 我正在尝试这样做:

MyCustomDialogFragment myCustomDialogFragment = new MyCustomDialogFragment();

// Since the onCreate() method of my custom DialogFragment returns an android.app.Dialog, I thought I could do this:

Dialog dialog = new CustomDialogFragment();

当然,这确实有效,我得到了:

Incompatible types.

    Required: android.app.Dialog

    Found: com.me.MyCustomDialogFragment

错误,即使 onCreate() 方法执行 return android.app.Dialog。我什至无法将其转换为 Dialog。那么,我怎样才能实现上面的 3 个目标呢?

那里有 "a lot" 个示例,但它们都只是默认 Google 示例的变体,不是很详尽。

how can I accomplish my 3 goals from above?

  1. Open a dialog.

在您的 MyCustomDialogFragment 实例上调用 show(),传入 FragmentManager。虽然 the framework DialogFragment is deprecated,它的 JavaDocs 展示了如何使用 show().

  1. Allow the user to input two strings within that dialog.

你的布局似乎提供了这个,虽然你会想要摆脱你的按钮,因为 AlertDialog 提供了这些。

  1. Click an "Okay" button, and have the two strings passed back to the calling activity.

Call setPositiveButton() 在你的 AlertDialog.Builder 上。让你传递给 setPositiveButton()DialogInterface.OnClickListener 从你的 EditText 小部件中获取值并将该数据获取到你的 activity (例如,使用共享 ViewModel在此片段和 activity 之间,并使用 EditText 内容更新 ViewModel 中的片段 LiveData

This sample app 展示了使用 DialogFragment 的基础知识,尽管使用的是 since-deprecated 框架 DialogFragment 实现而不是使用 ViewModel.