[英]is there a way to interact with a custom layout inflated in an Alert Dialog?
I have a code which inflates a custom layout inside an Alert Dialog.我有一个代码可以在警报对话框中扩展自定义布局。 The Custom Layout consists of an EditText and a Button.
自定义布局由 EditText 和 Button 组成。 What is the correct way to display the EditText String inside a Toast by clicking the Button?
通过单击按钮在 Toast 中显示 EditText 字符串的正确方法是什么?
I have tried the below code but it doesn't seem to work.我已经尝试了下面的代码,但它似乎不起作用。
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 file 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>
Create a separate function for toast为 toast 创建一个单独的 function
public void show_toast(){
Toast.makeText(
TestActivity.this,
dialogNameEditText.getText().toString(),
Toast.LENGTH_SHORT
).show();
}
and in onclicklistener call it并在 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();
});
});
Try this simple code.试试这个简单的代码。 it will work.
它会起作用的。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.