我创建了一个自定义对话框,在该对话框中使用自定义布局放大对话框视图,该对话框已成功显示,但是当我尝试关闭该对话框时,没有任何反应我尝试将对话框声明为最终对话框,但没有救命。
请查看我的代码:
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.fragment_itemdet, null);
dialogBuilder.setView(dialogView);
name=dialogView.findViewById(R.id.itemname);
img=dialogView.findViewById(R.id.itemdetimg);
counter=dialogView.findViewById(R.id.counter);
add=dialogView.findViewById(R.id.add);
del=dialogView.findViewById(R.id.sub);
confirm=dialogView.findViewById(R.id.confirm);
Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Poppins-ExtraBoldItalic.ttf");
name.setTypeface(custom_font);
final AlertDialog alertDialog = dialogBuilder.create();
add.setOnClickListener(v -> counter.setText(String.valueOf(i++)));
del.setOnClickListener(v -> counter.setText(String.valueOf(i--)));
name.setText(docname);
Glide.with(getContext()).load(url).into(img);
confirm.setOnClickListener(v->{
String qty=counter.getText().toString();
if (qty.equals("0")){
Toast.makeText(getContext(),"Please Specify Quantity",Toast.LENGTH_LONG).show();
}
else {
db=FirebaseFirestore.getInstance();
db.collection("Order").document(TableListFragment.tableno)
.update(
"Items", FieldValue.arrayUnion(docname),
"Quantity", FieldValue.arrayUnion(qty)
).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getContext(),"Item Added",Toast.LENGTH_LONG).show();
alertDialog.dismiss();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("WhatdFuck:",e.toString());
}
});
}
});
dialogBuilder.setTitle("Order");
dialogBuilder.setMessage("Enter Order Details");
final AlertDialog b = dialogBuilder.create();
b.show();
}
});
答案 0 :(得分:0)
像这样将您的alertDialog.dismiss();
移动到if (qty.equals("0")){
上方:
alertDialog.dismiss();
if (qty.equals("0")){...
更新
发生此问题是因为您正在显示不同的对话框(b),并关闭了不同的对话框(alertDialog):
b
和alertDialog
都是不同的对话框,因此请替换这两行:
final AlertDialog b = dialogBuilder.create();
b.show();
仅与此:
alertDialog.show();
位于代码底部。
答案 1 :(得分:0)
您还可以使用Dialog代替AlertDialog.Builder,如下所示。您也可以像这样自定义;
final Dialog dialog = new Dialog(MyFirebaseMessagingService.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_my_tasks);
dialog.setCancelable(true);
TextView tv_msg = dialog.findViewById(R.id.tv_msg);
TextView tv_reachedLoc = dialog.findViewById(R.id.tv_reachedLoc);
tv_msg .setText("");
tv_reachedLoc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// dismiss dialog
dialog.dismiss();
// do your task here
}
});
dialog.show();
}
答案 2 :(得分:0)
尝试使用此代码
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.fragment_itemdet);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setGravity(Gravity.CENTER);
dialog.show();
name=dialog.findViewById(R.id.itemname);
img=dialog.findViewById(R.id.itemdetimg);
counter=dialog.findViewById(R.id.counter);
add=dialog.findViewById(R.id.add);
del=dialog.findViewById(R.id.sub);
confirm=dialog.findViewById(R.id.confirm);
Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Poppins-ExtraBoldItalic.ttf");
name.setTypeface(custom_font);
add.setOnClickListener(v -> counter.setText(String.valueOf(i++)));
del.setOnClickListener(v -> counter.setText(String.valueOf(i--)));
name.setText(docname);
Glide.with(getContext()).load(url).into(img);
confirm.setOnClickListener(v->{
dialog.dismiss();
String qty=counter.getText().toString();
if (qty.equals("0")){
Toast.makeText(getContext(),"Please Specify Quantity",Toast.LENGTH_LONG).show();
}
else {
db=FirebaseFirestore.getInstance();
db.collection("Order").document(TableListFragment.tableno)
.update(
"Items", FieldValue.arrayUnion(docname),
"Quantity", FieldValue.arrayUnion(qty)
).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getContext(),"Item Added",Toast.LENGTH_LONG).show();
alertDialog.dismiss();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("WhatdFuck:",e.toString());
}
});
}
});
}
});
答案 3 :(得分:0)
问题在于您在两个不同的地方调用dialogBuilder.create,而alertDialog实例从未显示。
删除行:
...b = dialogBuilder.create();
b.show();
并在同一位置添加
alertDialog.show();
此外,您还必须修正所有内容的顺序,以便其正常工作。
答案 4 :(得分:0)
尝试关闭UI线程中的对话框
runOnUiThread(new Runnable() {
@Override
public void run() {
alertDialog.dismiss();
}
});
答案 5 :(得分:0)
您只需要使用
alertDialog.show();
代替这两行
final AlertDialog b = dialogBuilder.create();
b.show();
答案 6 :(得分:0)
替换最后两行代码。当您创建对话框的两个实例并尝试关闭未显示的对话框时,会产生问题。
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.fragment_itemdet, null);
dialogBuilder.setView(dialogView);
name = dialogView.findViewById(R.id.itemname);
img = dialogView.findViewById(R.id.itemdetimg);
counter = dialogView.findViewById(R.id.counter);
add = dialogView.findViewById(R.id.add);
del = dialogView.findViewById(R.id.sub);
confirm = dialogView.findViewById(R.id.confirm);
Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Poppins-ExtraBoldItalic.ttf");
name.setTypeface(custom_font);
final AlertDialog alertDialog = dialogBuilder.create();
add.setOnClickListener(v -> counter.setText(String.valueOf(i++)));
del.setOnClickListener(v -> counter.setText(String.valueOf(i--)));
name.setText(docname);
Glide.with(getContext()).load(url).into(img);
confirm.setOnClickListener(v -> {
String qty = counter.getText().toString();
if (qty.equals("0")) {
Toast.makeText(getContext(), "Please Specify Quantity", Toast.LENGTH_LONG).show();
} else {
db = FirebaseFirestore.getInstance();
db.collection("Order").document(TableListFragment.tableno)
.update(
"Items", FieldValue.arrayUnion(docname),
"Quantity", FieldValue.arrayUnion(qty)
).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(getContext(), "Item Added", Toast.LENGTH_LONG).show();
alertDialog.dismiss();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("WhatdFuck:", e.toString());
}
});
}
});
dialogBuilder.setTitle("Order");
dialogBuilder.setMessage("Enter Order Details");
alertDialog.show();