下面列出了android.app.Dialog#setOnCancelListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean checkGooglePlayServicesAvailability(Activity activity) {
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int resultCode = api.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (api.isUserResolvableError(resultCode)) {
Dialog dialog = api.getErrorDialog(activity, resultCode, 1234);
dialog.setCancelable(false);
dialog.setOnCancelListener(dialogInterface -> activity.finish());
dialog.show();
} else {
Toast.makeText(activity, "Device unsupported", Toast.LENGTH_LONG).show();
activity.finish();
}
return false;
}
return true;
}
/**
* A utility method to validate that the appropriate version of the Google Play Services is
* available on the device. If not, it will open a dialog to address the issue. The dialog
* displays a localized message about the error and upon user confirmation (by tapping on
* dialog) will direct them to the Play Store if Google Play services is out of date or missing,
* or to system settings if Google Play services is disabled on the device.
*
* @param activity
* @return
*/
public static boolean checkGooglePlayServices(final Activity activity) {
final int googlePlayServicesCheck = GooglePlayServicesUtil.isGooglePlayServicesAvailable(
activity);
switch (googlePlayServicesCheck) {
case ConnectionResult.SUCCESS:
return true;
default:
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(googlePlayServicesCheck,
activity, 0);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
activity.finish();
}
});
dialog.show();
}
return false;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog result = super.onCreateDialog(savedInstanceState);
result.requestWindowFeature(Window.FEATURE_NO_TITLE);
result.setOnCancelListener(this);
if (Build.VERSION.SDK_INT >= 21)
{
// set a background with round corners
result.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.dmfs_colorpicker_dialog_background));
// ensure we still have the right drop shadow in place
ViewCompat.setElevation(result.getWindow().getDecorView(), 24);
}
// else: on ancient devices we'll just continue using default square corners
return result;
}
/**
* A utility method to validate that the appropriate version of the Google Play Services is
* available on the device. If not, it will open a dialog to address the issue. The dialog
* displays a localized message about the error and upon user confirmation (by tapping on
* dialog) will direct them to the Play Store if Google Play services is out of date or missing,
* or to system settings if Google Play services is disabled on the device.
*/
public static boolean checkGooglePlayServices(final Activity activity) {
final int googlePlayServicesCheck = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
switch (googlePlayServicesCheck) {
case ConnectionResult.SUCCESS:
return true;
default:
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(googlePlayServicesCheck, activity, 0);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
activity.finish();
}
});
dialog.show();
}
return false;
}
/**
* Constructor.
*
* @param loadingDialog loading dialog
* @param ifShowDialog Show loadingDialog if true else not.
*/
public LoadingDialogObserver(Dialog loadingDialog, boolean ifShowDialog) {
this.loadingDialog = loadingDialog;
this.ifShowDialog = ifShowDialog;
if (loadingDialog != null)
loadingDialog.setOnCancelListener(this);
}
/**
* Constructor.
*
* @param loadingDialog loading dialog
* @param ifShowDialog Show loadingDialog if true else not.
*/
public LoadingDialogObserver(Dialog loadingDialog, boolean ifShowDialog) {
this.loadingDialog = loadingDialog;
this.ifShowDialog = ifShowDialog;
if (loadingDialog != null)
loadingDialog.setOnCancelListener(this);
}
/**
* Constructor.
*
* @param loadingDialog loading dialog
* @param ifShowDialog Show loadingDialog if true else not.
*/
public LoadingDialogObserver(Dialog loadingDialog, boolean ifShowDialog) {
this.loadingDialog = loadingDialog;
this.ifShowDialog = ifShowDialog;
if (loadingDialog != null)
loadingDialog.setOnCancelListener(this);
}
/**
*
* @Title showSheet
* @param activity
* 上下文环境
* @param actionSheetConfig
* ActionSheet配置项
* @param actionSheetItemSelected
* ActionSheet Item选中监听
* @param cancelListener
* 取消按钮的监听,无需监听可传null
* @return Dialog 返回类型
*/
@SuppressLint("NewApi")
public Dialog show(final Activity activity,
ActionSheetConfig actionSheetConfig,
final OnActionSheetItemSelected actionSheetItemSelected,
OnCancelListener cancelListener) {
final Dialog dlg = new Dialog(activity, R.style.ActionSheet);
LinearLayout layout = this.getLayout(activity, dlg, actionSheetConfig,
actionSheetItemSelected);
TextView mCancel = (TextView) layout.findViewById(R.id.cancel);
mCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
actionSheetItemSelected.onActionSheetItemSelected(activity, 0);
dlg.dismiss();
}
});
Window w = dlg.getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.x = 0;
final int cMakeBottom = -1000;
lp.y = cMakeBottom;
lp.gravity = Gravity.BOTTOM;
dlg.onWindowAttributesChanged(lp);
dlg.setCanceledOnTouchOutside(false);
if (cancelListener != null)
dlg.setOnCancelListener(cancelListener);
dlg.setContentView(layout);
dlg.show();
// 设置点击弹窗外部取消actionsheet
dlg.setCanceledOnTouchOutside(true);
return dlg;
}
/**
* Check if device has the correct Google Play Services version.
*
* @param activity Current activity.
* @param availability New instance of GoogleApiAvailability.
* @return True if there was a successful connection ot Google Play Services.
*/
public static boolean hasGooglePlayServices(@NonNull Activity activity, @NonNull GoogleApiAvailability availability) {
final int result = availability.isGooglePlayServicesAvailable(activity);
if (result == ConnectionResult.SUCCESS) {
return true;
} else {
final Dialog dialog = availability.getErrorDialog(activity, result, 0);
// Let user use the application
dialog.setOnCancelListener(DialogInterface::cancel);
dialog.show();
}
return false;
}
public SearchPrinterDialog(Activity activity, BTService service) {
this.activity = activity;
this.service = service;
deviceItems = new HashMap<>();
@SuppressLint("InflateParams")
View view = LayoutInflater.from(activity).inflate(R.layout.serach_dialog_layout, null, false);
insets = dpToPx(16);
activity.registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
activity.registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_CLASS_CHANGED));
activity.registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_NAME_CHANGED));
activity.registerReceiver(receiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
Set<BluetoothDevice> pairedDevices = service.getBondedDevices();
availableDevicesContainer = (LinearLayout) view.findViewById(R.id.available_devices_container);
progress = view.findViewById(R.id.search_devices_progress);
if (pairedDevices.size() > 0) {
fillPairedDevices(pairedDevices, (LinearLayout) view.findViewById(R.id.paired_devices_container));
} else {
view.findViewById(R.id.paired_devices).setVisibility(View.GONE);
}
service.startDiscovery();
dialog = new Dialog(activity);
dialog.setCancelable(true);
dialog.setContentView(view);
dialog.setTitle(R.string.pos_title_choose_printer);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
SearchPrinterDialog.this.onCancel();
}
});
dialog.show();
}
public void show(@NonNull Activity activity, Bundle bundle, int reqCode, @Nullable VKError vkError) {
mVkError = vkError;
mBundle = bundle;
mReqCode = reqCode;
mView = View.inflate(activity, R.layout.vk_open_auth_dialog, null);
mProgress = mView.findViewById(R.id.progress);
mWebView = (WebView) mView.findViewById(R.id.copyUrl);
final Dialog dialog = new Dialog(activity, R.style.VKAlertDialog);
dialog.setContentView(mView);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
dialog.dismiss();
}
});
dialog.setOnDismissListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dialog.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
mDialog = dialog;
mDialog.show();
loadPage();
}
private static void setDialogCloseHandler(final Dialog d, final Runnable callback, final boolean cancelOnly) {
final DialogCloseHandler handler = new DialogCloseHandler(callback, cancelOnly);
d.setOnCancelListener(handler);
d.setOnDismissListener(handler);
}
/**
*
* @Title showSheet
* @Description TODO(这里用一句话描述这个方法的作用)
* @param activity
* 上下文环境
* @param actionSheetConfig
* ActionSheet配置项
* @param actionSheetItemSelected
* ActionSheet Item选中监听
* @param cancelListener
* 取消按钮的监听,无需监听可传null
* @return Dialog 返回类型
*/
@SuppressLint("NewApi")
public Dialog show(final Activity activity,
ActionSheetConfig actionSheetConfig,
final OnActionSheetItemSelected actionSheetItemSelected,
OnCancelListener cancelListener) {
final Dialog dlg = new Dialog(activity, R.style.ActionSheet);
LinearLayout layout = this.getLayout(activity, dlg, actionSheetConfig,
actionSheetItemSelected);
TextView mCancel = (TextView) layout.findViewById(R.id.cancel);
mCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
actionSheetItemSelected.onActionSheetItemSelected(activity, 0);
dlg.dismiss();
}
});
Window w = dlg.getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.x = 0;
final int cMakeBottom = -1000;
lp.y = cMakeBottom;
lp.gravity = Gravity.BOTTOM;
dlg.onWindowAttributesChanged(lp);
dlg.setCanceledOnTouchOutside(false);
if (cancelListener != null)
dlg.setOnCancelListener(cancelListener);
dlg.setContentView(layout);
dlg.show();
// 设置点击弹窗外部取消actionsheet
dlg.setCanceledOnTouchOutside(true);
return dlg;
}