无法关闭自定义AlertDialog [英] Cannot close a custom AlertDialog
问题描述
因此,我有一个带有三个不同按钮(父按钮)的应用程序,按下它们时,会显示具有9个不同按钮的自定义警报对话框,但是警报对话框中这9个按钮的功能会有所不同,具体取决于哪个三个父按钮将其称为.按下9个按钮中的任何一个时,我希望该应用程序执行特定功能,然后关闭alertdialog.现在的问题是,我可以通过调用我创建的方法 showcustomdialog();
轻松地调用警报对话框,但是不能使用 alertdialog.dismiss();
在父按钮的OnClickListener内,因为该方法的结果类型为空.我试过使用if-else语句,但是它不起作用.我该如何达到要求?
so I have an app with three different buttons(parent buttons) on pressing any of them, the custom alert dialog with 9 different buttons is displayed but the functions of these 9 buttons within the alert dialog are different depending on which of the three parent buttons called it. On pressing any of the 9 buttons, I want the app to perform a particular function and then close the alertdialog. Now the problem is that I can easily call the alert dialog by calling the method showcustomdialog();
that I created but I can't dismiss it using alertdialog.dismiss();
inside the OnClickListener of the parent button since the method has a void result type. I've tried using if-else statements but it doesn't work. How can I achieve what is required?
方法:
private void showCustomDialog() {
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_main2, viewGroup, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
我正在呼叫肉类并按如下方式使用它:
I am calling the meathod and using it as follows:
parentbutton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showCustomDialog();
alertbutton1.getId();
alertbutton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView1.setText("500");
function();
//I want to dismiss the alertdialog here.
}
});
alertbutton2.getId();
alertbutton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView1.setText("1000");
function();
//I want to dismiss the alertdialog here.
}
});
以此类推.
推荐答案
我使用AlertDialog为您找到了解决方案.
I figured out a solution for you using AlertDialog.
parentbutton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createDialog();
}
});
然后方法必须是私有或公共AlertDialog:
Then the method MUST be a private or public AlertDialog:
private AlertDialog createDialog() {
LayoutInflater inflater = (getActivity()).getLayoutInflater(); // for fragment or getLayoutInflater(); for activity
View v = inflater.inflate(R.layout.dialog_add_new_list, null);
Button okButton = v.findViewById(R.id.confirm_button);
Button cancelButton = v.findViewById(R.id.cancel_button);
final AlertDialog dialog = new AlertDialog.Builder(getActivity()) // for fragment or AlertDialog.Builder ( this ) for activity
.setView(v)
.show();
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View p1) {
dialog.dismiss();
}
});
return dialog;
}
P.S.将您用来创建此示例的ID替换为您的ID.
P.S. Replace the ids I used to create this sample with yours.
这篇关于无法关闭自定义AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!