【问题标题】:Inflate Layout to alert dialog android studio膨胀布局以提醒对话框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_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>

【问题讨论】:

  • 您需要在AlertDialog.Builder 上拨打show(),就在setPositiveButton() 通话之后。

标签: java android android-layout android-alertdialog android-inflate


【解决方案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 -> 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多