首页 > 解决方案 > 膨胀布局以提醒对话框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>

标签: javaandroidandroid-layoutandroid-alertdialogandroid-inflate

解决方案


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

推荐阅读