【问题标题】:How to get value of material dialog box input field in Android?如何在Android中获取材质对话框输入字段的值?
【发布时间】:2021-08-16 09:07:45
【问题描述】:

我有一个像这样的材料警告对话框:

这里是对话框的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);

问题是,当我试图将这些输入字段的值作为字符串获取时,它们什么也没有返回。这里有什么问题?这是因为 findViewById() 无法找到字段,因为它们在警报对话框内而不是在实际活动中?

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

【问题讨论】:

    标签: java android xml material-components-android


    【解决方案1】:

    这是因为 findViewById() 无法按原样找到字段吗 在警报对话框内而不是在实际活动中?

    是的,你是对的 :) 你需要在你设置到你的 dialogBu​​ilder 的视图中获取它们。更改设置对话框视图的方式:

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

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

    【讨论】:

      猜你喜欢
      • 2015-02-27
      • 1970-01-01
      • 2021-10-07
      • 2019-11-20
      • 2015-12-24
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多