膨胀布局以提醒对话框android studio
Posted
技术标签:
【中文标题】膨胀布局以提醒对话框android studio【英文标题】:Inflate Layout to alert dialog android studio 【发布时间】:2021-10-27 00:28:31 【问题描述】:我有以下方法,我试图在更改对话框中膨胀布局,但没有任何反应。我正在构建待办事项应用程序,我想在单击添加图像按钮时显示对话框。 我需要你的帮助来解决这个问题。 任何帮助将不胜感激:)
imageButton.setOnClickListener(v ->
Toast.makeText(MainActivity.this, "Button add clicked ", Toast.LENGTH_SHORT).show();
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewInput = inflater.inflate(R.layout.note_input,null,false);
EditText editTitle = viewInput.findViewById(R.id.edit_text);
EditText editDescription = viewInput.findViewById(R.id.edit_description);
new AlertDialog.Builder(MainActivity.this)
.setView(viewInput)
.setTitle("Add Note")
.setPositiveButton("Add", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String title = editTitle.getText().toString();
String description = editDescription.getText().toString();
Note note = new Note(title,description);
boolean isInserted = new NoteHandler(MainActivity.this).create(note);
if(isInserted)
Toast.makeText(MainActivity.this, "Note saved", Toast.LENGTH_SHORT).show();
loadNotes();
else
Toast.makeText(MainActivity.this, "Unable to save the note", Toast.LENGTH_SHORT).show();
dialog.cancel();
);
);
XML 文件
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_>
<EditText
android:id="@+id/edit_text"
android:layout_
android:layout_
android:hint="Note title"
app:layout_constraintBottom_toTopOf="@+id/edit_description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edit_description"
android:layout_
android:layout_
android:layout_marginTop="52dp"
android:hint="Note description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
您需要在AlertDialog.Builder
上拨打show()
,就在setPositiveButton()
通话之后。
【参考方案1】:
new AlertDialog.Builder(MainActivity.this)
.setView(viewInput)
.setTitle("Add Note")
.setPositiveButton("Add", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String title = editTitle.getText().toString();
String description = editDescription.getText().toString();
Note note = new Note(title,description);
boolean isInserted = new NoteHandler(MainActivity.this).create(note);
if(isInserted)
Toast.makeText(MainActivity.this, "Note saved", Toast.LENGTH_SHORT).show();
loadNotes();
else
Toast.makeText(MainActivity.this, "Unable to save the note", Toast.LENGTH_SHORT).show();
dialog.cancel();
).show();//Need to ad .show() right here
【讨论】:
Drivers Sea -> 非常感谢以上是关于膨胀布局以提醒对话框android studio的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio APP开发入门之对话框Dialog的讲解及使用(附源码 包括提醒对话框,日期对话框,时间对话框)