有没有办法与警报对话框中膨胀的自定义布局进行交互?

is there a way to interact with a custom layout inflated in an Alert Dialog?

我有一段代码可以在警报对话框中扩充自定义布局。 自定义布局由一个 EditText 和一个 Button 组成。通过单击按钮在 Toast 中显示 EditText 字符串的正确方法是什么?

我试过下面的代码,但它似乎不起作用。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Fetch", (dialog, which) -> {

}).setNeutralButton("Cancel", (dialog, which) -> {

});

final AlertDialog dialog = builder.create();

LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.custom_layout, null);

dialog.setView(dialogLayout);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();

dialog.setOnShowListener(d -> {
    EditText dialogNameEditText = dialog.findViewById(R.id.dialog_name_edittext);
    Button dialogPrintButton = dialog.findViewById(R.id.dialog_print_button);

    dialogPrintButton.setOnClickListener(v1 -> {
        Toast.makeText(
                TestActivity.this,
                dialogNameEditText.getText().toString(),
                Toast.LENGTH_SHORT
        ).show();
    });
});

custom_layout.xml 文件

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="12dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <EditText
        android:id="@+id/dialog_name_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorDark"
        android:ems="10"
        android:hint="Name"
        android:inputType="text"
        android:padding="10dp"
        android:textColor="@color/colorWhite"
        android:textColorHint="@color/colorLight" />

    <Button
        android:id="@+id/dialog_print_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/colorPrimary"
        android:text="Print"
        android:textColor="@color/colorLight" />
    
</LinearLayout>

Originally answered

为 toast 创建一个单独的函数

public void show_toast(){
Toast.makeText(
                TestActivity.this,
                dialogNameEditText.getText().toString(),
                Toast.LENGTH_SHORT
        ).show();
}

并在 onclicklistener 中调用它

dialog.setOnShowListener(d -> {
    EditText dialogNameEditText = dialog.findViewById(R.id.dialog_name_edittext);
    Button dialogPrintButton = dialog.findViewById(R.id.dialog_print_button);

    dialogPrintButton.setOnClickListener(v1 -> {
        show_toast();
    });
});

试试这个简单的代码。它会起作用。

 Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_layout);

    EditText dialogNameEditText = dialog.findViewById(R.id.dialog_name_edittext);
    Button dialogPrintButton = dialog.findViewById(R.id.dialog_print_button);
    dialogPrintButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,dialogNameEditText.getText(), Toast.LENGTH_SHORT).show();
        }
    });
    dialog.show();