问题
I have a java class not activity. From any other activity, I called this class and in that class I created alertdialog builder. In that I inflate data from db.
Now In this class, I have other listener and method also. In one of the method, I want to dismiss/cancel this dialog. Like how we do
setResult(RESULT_OK, intent);
finish();
in any activity, same thing I want to do in class.
Code: This method I called from activity.
public void showProvidersDialog(long customCategoryId) {
categoryId = customCategoryId;
LayoutInflater li = LayoutInflater.from(context);
promptsView = li.inflate(R.layout.row_providers_layout, null);
init();
alertDialogBuilder = new android.app.AlertDialog.Builder(context, R.style.dialogBoxStyle);
alertDialogBuilder.setView(promptsView);
alertDialogBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
isInsurance();
alertDialogBuilder.show();
//solved: dialog = alertDialogBuilder.create();
dialog.show();
}
And I have one more method in same java class, from that method I want to dismiss currentlt opened dialog.
private void sendProviderData(General provider) {
Singleton.getInstance().setProviderId(provider.getId());
Singleton.getInstance().setProviderIcon(provider.getIcon());
Singleton.getInstance().setProviderName(provider.getName());
//solved
dialog.dismiss
}
Explain once again: See, I can cancel dialog inside negative button. But what I want is, in that dialog I inflate row which includes one contact list. I want that when user click on any of the contact (which is let's say clicked on recycler on touch listener) I'm passing some data using singleton and at that same time, I want to dismiss dialog.
回答1:
Here's dialog code for generic purpose. You can call whenever you need to show the dialog. You can show dialog by calling showDialog()
method & dismiss by calling dismissDialog()
method.
/*
* call whenever dialog is required in whole app in form of popup
*/
public class MyDialog implements View.OnClickListener {
private Dialog dialog;
private Context context;
private TextView tvTitle;
private TextView tvSubtitle;
private Button bt_ok;
private String strInvalidUserNamePass, strHeader;
/*
* constructor to change the text dynamically.
*/
public MyDialog(Context context, String strHeader, String invalidUserNamePass) {
this.context = context;
this.strInvalidUserNamePass = invalidUserNamePass;
this.strHeader = strHeader;
if (context != null) {
initDialog();
}
}
/*
* set id of all the view components and implement listeners
*/
private void initDialog() {
dialog = new Dialog(context, R.style.FMDialogNormal);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_validation);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
dialog.show();
tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
tvSubtitle = (TextView) dialog.findViewById(R.id.tv_subtitle);
tvTitle.setText(strHeader);
tvSubtitle.setText(strInvalidUserNamePass);
bt_ok = (Button) dialog.findViewById(R.id.bt_ok);
bt_ok.setOnClickListener(this);
}
/*
* Implement listener according to the views
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_ok:
dialog.dismiss();
break;
}
}
public void showDialog(){
if(dialog!=null){
dialog.show();
}
}
public void dismissDialog(){
if(dialog!=null && isVisible()){
dialog.show();
}
}
public boolean isVisible() {
if (dialog != null) {
return dialog.isShowing();
}
return false;
}
}
来源:https://stackoverflow.com/questions/56816466/cancel-dismiss-alertdialog-builder-from-any-other-method-in-same-class-in-androi