【发布时间】: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