java - 膨胀布局以提醒对话框android studio
问题描述
我有以下方法,我试图在改变对话框中膨胀布局,但没有任何反应。我正在构建待办事项应用程序,我想在单击添加图像按钮时显示对话框。我需要你的帮助来解决这个问题。任何帮助将不胜感激 :)
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();
}
});
});
文件
<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_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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>
解决方案
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
推荐阅读
- r - 在不使用 R 中的内置函数的情况下生成几何变量
- json - 如何使用 NimYAML 将 JsonNode 转储到 yaml 文件?
- xamarin.forms - Xamarin Forms:删除网格元素之间的间隙
- android - 我正在创建 react-native 应用程序,我的 api 是在节点中开发的,我正在尝试从 API 获取数据,但我做不到
- keras - GridSearchCV - 无需改装的最佳模型
- vb.net - 具有下推效果的按钮的代码?VB网
- ios - 父对话框中 KeyboardAvoidingView 的反应原生内容
- javascript - 无法在专用网络上获取 /peerjs/id
- protocol-buffers - Protobuf 组和消息
- sql - PL/SQL 抛出错误 ORA-02289: 序列不存在 (Oracle SQL)