下面列出了android.content.DialogInterface.OnKeyListener#android.support.v7.app.AlertDialog 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void requestPermissions(int title, int message, final int requestCode, final String... permissions) {
boolean shouldShowDialog = false;
for (String permission : permissions) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
shouldShowDialog = true;
break;
}
}
if (shouldShowDialog) {
final Activity activity = this;
AlertDialog builder = new AlertDialog.Builder(this).setTitle(title)
.setMessage(message)
.setPositiveButton(android.R.string.ok, null).create();
builder.setCanceledOnTouchOutside(false);
builder.show();
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
ActivityCompat.requestPermissions(activity, permissions, requestCode);
}
});
} else
ActivityCompat.requestPermissions(this, permissions, requestCode);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
final View alertDialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialog_about, null);
final AlertDialog alertDialog = alertDialogBuilder.setView(alertDialogView).setPositiveButton(getString(R.string.ok), null).show();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return alertDialog;
}
private void showMLSLimitedServiceDisclaimer() {
int initialGuideVersion = PreferenceManager.getDefaultSharedPreferences(getBaseContext())
.getInt(Constants.APP_INITIAL_GUIDE_VERSION, 0);
if (initialGuideVersion != 4) {
return;
}
final Context localContext = getBaseContext();
final AlertDialog.Builder settingsAlert = new AlertDialog.Builder(MainActivity.this);
settingsAlert.setTitle(R.string.alertDialog_mls_service_title);
settingsAlert.setMessage(R.string.alertDialog_mls_service_message);
settingsAlert.setNeutralButton(R.string.alertDialog_battery_optimization_proceed,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor preferences = PreferenceManager.getDefaultSharedPreferences(localContext).edit();
preferences.putInt(Constants.APP_INITIAL_GUIDE_VERSION, 5);
preferences.apply();
checkAndShowInitialGuide();
}
});
settingsAlert.show();
}
public void showDeleteAllPhotosAlertDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(getString(R.string.remove_all_tagged_posts));
alert.setMessage(getString(R.string.are_you_sure_you_want_to_delete_all_posts));
alert.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
InstaTagApplication.getInstance().savePhotos(new ArrayList<Photo>());
photos.clear();
photoAdapter.notifyDataSetChanged();
showEmptyContainer();
}
});
alert.setNegativeButton(getString(R.string.cancel), null);
alert.show();
}
private void initExitDialog(OnExitedListener listener) {
OnClickListener onClickListener = v -> {
switch (v.getId()) {
case R.id.continue_game_btn:
mExitDialog.dismiss();
break;
case R.id.back_to_menu_btn:
mExitDialog.dismiss();
exitNow();
listener.onExited();
break;
default:
break;
}
};
View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_exit_view, null, false);
dialogView.findViewById(R.id.continue_game_btn).setOnClickListener(onClickListener);
dialogView.findViewById(R.id.back_to_menu_btn).setOnClickListener(onClickListener);
mExitDialog = new AlertDialog.Builder(getContext(), R.style.DialogTheme).setView(dialogView).create();
}
private void showDisclaimerForFirstTime() {
final SharedPreferences preferences = getActivity().getSharedPreferences("temp", Context.MODE_PRIVATE);
final String hasShown = "hasShown";
if (preferences.getBoolean(hasShown, false)) {
return;
}
AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
.setTitle("This is still in beta!")
.setMessage("Data is not yet finished nor complete. Please help us via Github. Links available in the drawer.")
.setPositiveButton("Ok no problem!", null)
.show();
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
preferences.edit().putBoolean(hasShown, true).apply();
}
});
}
private void showSearchDialog(){
final EditText editText = new EditText(this);
editText.setGravity(Gravity.CENTER);
editText.setSingleLine();
new AlertDialog.Builder(this)
.setTitle(getString(R.string.text_search_books))
.setIcon(R.mipmap.ic_search)
.setView(editText)
.setPositiveButton(getString(R.string.text_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(Utils.hasString(editText.getText().toString())){
Intent intent = new Intent(MainActivity.this,SearchBooksActivity.class);
Bundle bundle = new Bundle();
bundle.putString(getString(R.string.id_search_text),editText.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
}).show();
}
private ColorPickerDialogBuilder(Context context, int theme) {
defaultMargin = getDimensionAsPx(context, R.dimen.default_slider_margin);
final int dialogMarginBetweenTitle = getDimensionAsPx(context, R.dimen.default_slider_margin_btw_title);
builder = new AlertDialog.Builder(context, theme);
pickerContainer = new LinearLayout(context);
pickerContainer.setOrientation(LinearLayout.VERTICAL);
pickerContainer.setGravity(Gravity.CENTER_HORIZONTAL);
pickerContainer.setPadding(defaultMargin, dialogMarginBetweenTitle, defaultMargin, defaultMargin);
LinearLayout.LayoutParams layoutParamsForColorPickerView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
layoutParamsForColorPickerView.weight = 1;
colorPickerView = new ColorPickerView(context);
pickerContainer.addView(colorPickerView, layoutParamsForColorPickerView);
builder.setView(pickerContainer);
}
private void askForOSMUsername() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("OpenStreetMap User Name");
builder.setMessage("Please enter your OpenStreetMap user name.");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String userName = input.getText().toString();
SharedPreferences.Editor editor = userNamePref.edit();
editor.putString("userName", userName);
editor.apply();
if (TagEdit.saveToODKCollect(userName)) {
setResult(Activity.RESULT_OK);
finish();
}
}
});
builder.show();
}
@Override
public void showError(Throwable throwable) {
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle(getString(R.string.error_dialog_title))
.setMessage(
throwable.getCause() instanceof ConnectException ?
getString(R.string.general_error) :
throwable.getLocalizedMessage()
)
.setCancelable(true)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
if (throwable.getCause() instanceof ConnectException) {
// finish();
}
})
.create();
alertDialog.show();
}
public static void showPrivacyPolicyDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogTheme);
TextView msg = new TextView(context);
msg.setText(Html.fromHtml(Utils.privacyPolicy));
msg.setMovementMethod(LinkMovementMethod.getInstance());
builder.setView(msg);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
lp.setMargins(50, 50, 50, 50);
msg.setLayoutParams(lp);
ScrollView ll = new ScrollView(context);
ll.addView(msg);
builder.setView(ll);
// builder.setMessage("声明:\n所有用户的密码将不会被开发者获取,如仍有疑问,可访问\"https://github.com/cbwang2016/PKUCourses\"查看源码,谢谢您的信任。");
builder.setPositiveButton("好的", null).create().show();
}
private void onWifiDisabled() {
log.d("Wi-Fi disabled; prompting user");
new AlertDialog.Builder(this)
.setTitle(R.string.wifi_required)
.setPositiveButton(R.string.enable_wifi, (dialog, which) -> {
dialog.dismiss();
log.i("Enabling Wi-Fi at the user's request.");
wifiFacade.setWifiEnabled(true);
wifiListFragment.scanAsync();
})
.setNegativeButton(R.string.exit_setup, (dialog, which) -> {
dialog.dismiss();
finish();
})
.show();
}
private void eoou2(String dec) {
//com.tencent.mm.plugin.scanner.ui.BaseScanUI
final SharedPreferences i = this.getSharedPreferences("Hero", 0);
Boolean o0 = i.getBoolean("FIRST", true);
if (o0) {//第一次
AppCompatDialog alertDialog = new AlertDialog.Builder(ws_Main3Activity.this)
.setTitle("快捷功能")
.setMessage("需要ROOT才能使用")
.setNeutralButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
i.edit().putBoolean("FIRST", false).apply();
}
}).setCancelable(false).create();
alertDialog.show();
} else {
ShellUtils.execCommand("am start -n com.eg.android.AlipayGphone/" + dec, true, true);
}
}
/**
* 拍照点击事件拦截
*
* @param activity 当前activity
* @param takePhoto 拍照接口
* @return 是否拦截
*/
@Override
public boolean interceptCameraClick(@Nullable final Activity activity, final ICameraExecutor takePhoto) {
if (activity == null || activity.isDestroyed()) {
return false;
}
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setSingleChoiceItems(new String[]{"拍照", "录像"}, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (which == 0) {
takePhoto.takePhoto();
} else {
takePhoto.takeVideo();
}
}
});
builder.show();
return true;
}
@Override
public void didRegistrationError(Throwable error) {
dismissProgressDialog();
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle(getString(R.string.error_dialog_title))
.setMessage(error.getCause() instanceof ConnectException ?
getString(R.string.general_error) :
error.getLocalizedMessage())
.setCancelable(true)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
if (error.getCause() instanceof ConnectException) {
finish();
}
})
.create();
alertDialog.show();
}
private void init() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.fragment_result, null);
builder.setCancelable(false)
.setView(view)
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (mRunnable != null) mRunnable.run();
}
});
mResultDialog = builder.create();
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View rootView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_portrait_seletor_layout, null);
RecyclerView recyclerView = rootView.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 5));
PortraitAdapter adapter = new PortraitAdapter();
recyclerView.setAdapter(adapter);
int padding = ViewUtils.dp2Px(getContext(), 2);
recyclerView.addItemDecoration(new SpaceItemDecoration(padding, padding, padding, padding));
return new AlertDialog.Builder(getContext())
.setTitle(R.string.pick_portrait)
.setNegativeButton(R.string.text_cancel, null)
.setView(rootView)
.create();
}
@Override public void showAddCheckPointDialog() {
LatLng target = mMap.getCameraPosition().target;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View dialogView =
LayoutInflater.from(context).inflate(R.layout.set_checkpoint_dialog_view, null, false);
((TextView) dialogView.findViewById(R.id.check_point_lat_tv)).setText(
String.valueOf(target.latitude));
((TextView) dialogView.findViewById(R.id.check_point_long_tv)).setText(
String.valueOf(target.longitude));
EditText nameEditText = dialogView.findViewById(R.id.check_point_name_et);
AlertDialog alertDialog = builder.setView(dialogView).show();
RxView.clicks(dialogView.findViewById(R.id.set_checkpoint_done_btn)).subscribe(__ -> {
String enteredText = nameEditText.getText().toString();
if (enteredText.length() >= 4) {
locationPresenter.onSetCheckPoint(enteredText, target.latitude, target.longitude);
alertDialog.dismiss();
} else {
nameEditText.setError("Name should have minimum of 4 characters.");
}
});
RxView.clicks(dialogView.findViewById(R.id.set_checkpoint_cancel_btn))
.subscribe(__ -> alertDialog.dismiss());
}
@Override @NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String[] repeatArray = getResources().getStringArray(R.array.repeat_array);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.Dialog);
builder.setItems(repeatArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == Reminder.SPECIFIC_DAYS) {
DialogFragment daysOfWeekDialog = new DaysOfWeekSelector();
daysOfWeekDialog.show(getActivity().getSupportFragmentManager(), "DaysOfWeekSelector");
} else if (which == Reminder.ADVANCED) {
DialogFragment advancedDialog = new AdvancedRepeatSelector();
advancedDialog.show(getActivity().getSupportFragmentManager(), "AdvancedSelector");
} else {
listener.onRepeatSelection(RepeatSelector.this, which, repeatArray[which]);
}
}
});
return builder.create();
}
/**
* Shows a warning dialog indicating that download has failed. When user confirms
* the warning, a message will be sent to the notification server to inform about the
* error.
*
* @param titleId The resource identifier for the title.
* @param omaInfo Information about the OMA content.
* @param downloadInfo Information about the download.
* @param statusMessage Message to be sent to the notification server.
*/
private void showDownloadWarningDialog(
int titleId, final OMAInfo omaInfo, final DownloadInfo downloadInfo,
final String statusMessage) {
DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == AlertDialog.BUTTON_POSITIVE) {
sendInstallNotificationAndNextStep(omaInfo, downloadInfo,
DownloadItem.INVALID_DOWNLOAD_ID, statusMessage);
}
}
};
new AlertDialog.Builder(
ApplicationStatus.getLastTrackedFocusedActivity(), R.style.AlertDialogTheme)
.setTitle(titleId)
.setPositiveButton(R.string.ok, clickListener)
.setCancelable(false)
.show();
}
public static AlertDialog getProgressDialog(Activity context,String title, String message){
View view = LayoutInflater.from(context).inflate(R.layout.dialog_progress, null);
ProgressBar progressBar = (ProgressBar) view.findViewById(android.R.id.progress);
TextView textView = (TextView) view.findViewById(R.id.content);
//改变Progress的背景为MaterialDesigner规范的样式
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
progressBar.setIndeterminateDrawable(new CircularProgressDrawable(Utils.getColorWarp(context, R.color.colorAccent), context.getResources().getDimension(R.dimen.loading_border_width)));
}
final AlertDialog progressDialog = new AlertDialog.Builder(context)
.setTitle(title)
.setView(view).create();
//设置显示文字
textView.setText(message);
return progressDialog;
}
@OnClick(R.id.custom_tile_width_size)
public void onWidthClick() {
final NumberPicker view = new NumberPicker(this);
view.setMinValue(24);
view.setMaxValue(64);
view.setWrapSelectorWheel(false);
view.setValue(currentTileWidth);
new AlertDialog.Builder(this)
.setView(view)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(@NonNull DialogInterface dialog, int which) {
currentTileWidth = view.getValue();
widget.setTileWidthDp(currentTileWidth);
}
})
.show();
}
@Override public void showThemeChooseDialog() {
AlertDialog.Builder builder = DialogUtils.makeDialogBuilder(activity);
builder.setTitle(R.string.change_theme);
Integer[] res = new Integer[] { R.drawable.deep_purple_round,
R.drawable.brown_round, R.drawable.blue_round,
R.drawable.blue_grey_round, R.drawable.yellow_round,
R.drawable.red_round, R.drawable.pink_round,
R.drawable.green_round };
List<Integer> list = Arrays.asList(res);
ColorsListAdapter adapter = new ColorsListAdapter(getActivity(), list);
adapter.setCheckItem(
ThemeUtils.getCurrentTheme(activity).getIntValue());
GridView gridView = (GridView) LayoutInflater.from(activity)
.inflate(
R.layout.colors_panel_layout,
null);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridView.setCacheColorHint(0);
gridView.setAdapter(adapter);
builder.setView(gridView);
final AlertDialog dialog = builder.show();
gridView.setOnItemClickListener((parent, view, position, id) -> {
dialog.dismiss();
mSettingPresenter.onThemeChoose(position);
});
}
private void setSuccess(String uid, DatabaseReference ref) {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Processing...");
dialog.setCancelable(false);
dialog.show();
ref.child(uid).setValue("paypal").addOnSuccessListener(runnable -> {
dialog.dismiss();
doOnSuccess();
}).addOnFailureListener(runnable -> {
dialog.dismiss();
new AlertDialog.Builder(this)
.setTitle("Error")
.setMessage("Payment has completed successfully, but the app could not process it. Click on LEARN MORE to solve this error!")
.setCancelable(false)
.setPositiveButton("Learn More",(dialogInterface, i) -> {
helpClick(null);
})
.setNegativeButton("Cancel",null)
.show();
});
}
private void showNewVersionDialog(String content, final String fileName) {
new AlertDialog.Builder(this)
.setTitle("新版本提示")
.setMessage(content)
.setPositiveButton("更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String url = BuildConfig.BASE_URL + BuildConfig.DOWNLOAD_URL;
Uri uri = Uri.parse(String.format(Locale.CHINA, url, fileName));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
})
.setNegativeButton("知道了", null)
.show();
}
private void showChooseDialog() {
new AlertDialog.Builder(this).setItems(getResources().getStringArray(R.array.photo_choose),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
switch (i) {
case 0:
openCamera();
break;
case 1:
pickPhoto();
break;
}
}
}).show();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialog_startup_title)
.setNegativeButton(R.string.dialog_startup_negative_button, null)
.setPositiveButton(R.string.dialog_startup_positive_button, onPositiveButtonClickListener);
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View viewRoot = inflater.inflate(R.layout.dialog_startup, null);
HtmlTextView htmlTextView = (HtmlTextView) viewRoot.findViewById(R.id.html_text);
String htmlContent =
"<p>"+getString(R.string.html_text_purpose)+"</p>"+
"<p>"+getString(R.string.html_text_free)+"</p>"+
"<p>"+getString(R.string.html_text_donation)+"</p>";
htmlTextView.setHtml(htmlContent);
builder.setView(viewRoot);
return builder.create();
}
/**
* Add an income and expense record
* @param v
*/
public void OnAddRecordClick(View v){
final String[] items = {"收入", "支出"};
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setTitle("请选择添加类别");
alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Toast.makeText(SpendingActivity.this, items[i], Toast.LENGTH_SHORT).show();
Intent intent=new Intent(SpendingActivity.this,ExpenseProcesActivity.class);
intent.putExtra("strType", i);
SpendingActivity.this.startActivity(intent);
alertDialog_AddRecord.dismiss();
}
});
alertDialog_AddRecord = alertBuilder.create();
alertDialog_AddRecord.show();
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case HANDLER_OPERATION_SHOW:
progressbarArr[0].setVisibility(View.VISIBLE);
break;
case HANDLER_OPERATION_UPDATE:
progressbarArr[0].setProgress(msg.arg1);
break;
case HANDLER_OPERATION_HIDE:
break;
case HANDLER_OPERATION_COMPLETE:
if (activityArr[0].isFinishing()) return;
AlertDialog.Builder builder = new AlertDialog.Builder(activityArr[0]);
builder.setTitle(R.string.ttl_import_complete);
builder.setMessage((String) msg.obj);
// Set up the buttons
builder.setPositiveButton("OK", onDialogOkListenerArr[0]);
builder.show();
break;
}
}
/**
* 从文本导入本地配置
*/
private void loadLocalConfigSet() {
if(configSet==null){
AlertDialog.Builder builder=new AlertDialog.Builder(this)
.setTitle("配置导入")
.setMessage("还没有导出,先导出再来试试吧")
.setPositiveButton("我知道了",null);
builder.create().show();
}else{
mScheduleConfig.load(configSet);
Toast.makeText(this,"配置已生效",Toast.LENGTH_SHORT).show();
}
mTimetableView.updateView();
}