如何获取Android中material对话框输入框的值?

How to get value of material dialog box input field in Android?

我有一个 material 警告对话框,如下所示:

这是对话框的 java 代码:

public void openAddNoteDialog() {

    MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(MainActivity.this, R.style.dialogBoxTheme);
    builder.setTitle("Add note");
    builder.setBackground(getResources().getDrawable(R.drawable.dialog_box, null));
    builder.setIcon(R.drawable.ic_add_note);
    builder.setView(R.layout.addnote_dialog);
    builder.setPositiveButton("Add", ((dialogInterface, i) -> handleAddNote()));
    builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
    builder.show();

}

这是对话框中的布局:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/notenameFLD"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="260dp"
    android:layout_height="90dp"
    android:layout_gravity="center"
    android:layout_marginTop="25dp"
    android:hint="@string/notename"
    android:textColorHint="@color/white"
    app:boxStrokeColor="@color/white"
    app:counterEnabled="true"
    app:counterMaxLength="20"
    app:counterTextColor="@color/white"
    app:helperTextTextColor="@color/white"
    app:hintTextColor="@color/white">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/notenameEDITFLD"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/opensans_regular"
        android:maxLength="20"
        android:textColor="@color/white"
        android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/notecontentFLD"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="260dp"
    android:layout_height="90dp"
    android:layout_gravity="center"
    android:layout_marginBottom="25dp"
    android:hint="@string/notecontent"
    android:textColorHint="@color/white"
    app:boxStrokeColor="@color/white"
    app:counterEnabled="true"
    app:counterMaxLength="250"
    app:counterTextColor="@color/white"
    app:helperTextTextColor="@color/white"
    app:hintTextColor="@color/white">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/notecontentEDITFLD"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@font/opensans_regular"
        android:maxLength="250"
        android:textColor="@color/white"
        android:textSize="20sp" />
</com.google.android.material.textfield.TextInputLayout>

现在,我正尝试在名为 handleAddNote() 的其他函数中获取这些输入字段的值。

final TextInputEditText noteNameFLD = findViewById(R.id.notenameEDITFLD);
final TextInputEditText noteContentFLD = findViewById(R.id.notecontentEDITFLD);

问题是,当我尝试将这些输入字段的值作为字符串获取时,它们 return 什么也没有。这里有什么问题?这是因为 findViewById() 找不到字段,因为它们位于警报对话框内,而不是实际 activity?

Ps。抱歉,如果这令人困惑,我可以根据需要添加更多信息。

Is this because the findViewById() can't find the fields as they are inside of the alert dialog box and not in the actual activity?

是的,您是对的 :) 您需要将它们显示在您为 dialogBu​​ilder 设置的视图中。将您设置对话框视图的方式更改为:

final View dialogView = LayoutInflater.from(context).inflate(R.layout. addnote_dialog, null);
alertDialogBuilder.setView(dialogView);

然后,您可以使用 dialogView.findViewById() 查找视图。