下面列出了android.content.DialogInterface#BUTTON_NEUTRAL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onClick(View v) {
int curClickedBtnType = DialogInterface.BUTTON_NEGATIVE;
if (v == tvBtnCancel) {
dismiss();
} else if (v == tvBtnCommit) {
curClickedBtnType = DialogInterface.BUTTON_POSITIVE;
}
else if (v == ivBtnClose) {
dismiss();
curClickedBtnType = DialogInterface.BUTTON_NEUTRAL;
}
if(dialogClickListener != null){
dialogClickListener.onClick(this, curClickedBtnType);
}
}
private void showDirPickerDialogL() {
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
openDirPicker();
break;
case DialogInterface.BUTTON_NEUTRAL:
openDirPickerL();
break;
}
}
};
new AlertDialog.Builder(getActivity()).setMessage(R.string.settings_download_pick_dir_l)
.setPositiveButton(R.string.settings_download_continue, listener)
.setNeutralButton(R.string.settings_download_document, listener)
.show();
}
@Override
public final Button getButton(final int whichButton) {
switch (whichButton) {
case DialogInterface.BUTTON_POSITIVE:
return (positiveButton != null && positiveButton.getVisibility() == View.VISIBLE) ?
positiveButton : null;
case DialogInterface.BUTTON_NEGATIVE:
return (negativeButton != null && negativeButton.getVisibility() == View.VISIBLE) ?
negativeButton : null;
case DialogInterface.BUTTON_NEUTRAL:
return (neutralButton != null && neutralButton.getVisibility() == View.VISIBLE) ?
neutralButton : null;
default:
return null;
}
}
/**
* Sets a click listener or a message to be sent when the button is clicked.
* You only need to pass one of {@code listener} or {@code msg}.
*
* @param whichButton Which button, can be one of
* {@link DialogInterface#BUTTON_POSITIVE},
* {@link DialogInterface#BUTTON_NEGATIVE}, or
* {@link DialogInterface#BUTTON_NEUTRAL}
* @param text The text to display in positive button.
* @param listener The
* {@link DialogInterface.OnClickListener} to
* use.
* @param msg The {@link Message} to be sent when clicked.
*/
public void setButton(int whichButton, CharSequence text,
DialogInterface.OnClickListener listener, Message msg) {
if (msg == null && listener != null) {
msg = mHandler.obtainMessage(whichButton, listener);
}
switch (whichButton) {
case DialogInterface.BUTTON_POSITIVE:
mButtonPositiveText = text;
mButtonPositiveMessage = msg;
break;
case DialogInterface.BUTTON_NEGATIVE:
mButtonNegativeText = text;
mButtonNegativeMessage = msg;
break;
case DialogInterface.BUTTON_NEUTRAL:
mButtonNeutralText = text;
mButtonNeutralMessage = msg;
break;
default:
throw new IllegalArgumentException("Button does not exist");
}
}
private void tryGettingCookies() {
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
showRegisterDialog();
break;
case DialogInterface.BUTTON_NEGATIVE:
showAddCookieDialog();
break;
case DialogInterface.BUTTON_NEUTRAL:
doAction();
break;
}
}
};
new AlertDialog.Builder(getContext()).setTitle(R.string.no_cookies)
.setMessage(R.string.no_cookies_ac)
.setPositiveButton(R.string.register, listener)
.setNegativeButton(R.string.add_cookies, listener)
.setNeutralButton(R.string.i_dont_care, listener).show();
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
final String oldAccount = getSignedInAccountName();
switch (which) {
case DialogInterface.BUTTON_POSITIVE: // Signed in
final ListView lv = ((AlertDialog)dialog).getListView();
final String newAccount =
(String) lv.getItemAtPosition(lv.getCheckedItemPosition());
getSharedPreferences()
.edit()
.putString(PREF_ACCOUNT_NAME, newAccount)
.apply();
AccountStateChangedListener.onAccountSignedIn(oldAccount, newAccount);
if (mDependentPreference != null) {
mDependentPreference.setChecked(true);
}
break;
case DialogInterface.BUTTON_NEUTRAL: // Signed out
AccountStateChangedListener.onAccountSignedOut(oldAccount);
getSharedPreferences()
.edit()
.remove(PREF_ACCOUNT_NAME)
.apply();
break;
}
}
public Button getButton(int whichButton) {
switch (whichButton) {
case DialogInterface.BUTTON_POSITIVE:
return mButtonPositive;
case DialogInterface.BUTTON_NEGATIVE:
return mButtonNegative;
case DialogInterface.BUTTON_NEUTRAL:
return mButtonNeutral;
default:
return null;
}
}
/**
* Displays dialog asking user to accept/deny usage reporting.
*/
private void showUsageReportingDialog(RestApi restApi) {
final DialogInterface.OnClickListener listener = (dialog, which) -> {
try {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
restApi.setUsageReporting(true);
restApi.saveConfigAndRestart();
break;
case DialogInterface.BUTTON_NEGATIVE:
restApi.setUsageReporting(false);
restApi.saveConfigAndRestart();
break;
case DialogInterface.BUTTON_NEUTRAL:
Uri uri = Uri.parse("https://data.syncthing.net");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
break;
}
} catch (Exception e) {
Log.e(TAG, "showUsageReportingDialog:OnClickListener", e);
}
};
restApi.getUsageReport(report -> {
@SuppressLint("InflateParams")
View v = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_usage_reporting, null);
TextView tv = v.findViewById(R.id.example);
tv.setText(report);
Util.getAlertDialogBuilder(MainActivity.this)
.setTitle(R.string.usage_reporting_dialog_title)
.setView(v)
.setPositiveButton(R.string.yes, listener)
.setNegativeButton(R.string.no, listener)
.setNeutralButton(R.string.open_website, listener)
.show();
});
}
private void createAskUseOldDialog(final AndroidSessionWrapper state, final SessionStateDescriptor existing) {
final AndroidCommCarePlatform platform = CommCareApplication.instance().getCommCarePlatform();
String title = Localization.get("app.workflow.incomplete.continue.title");
String msg = Localization.get("app.workflow.incomplete.continue");
StandardAlertDialog d = new StandardAlertDialog(this, title, msg);
DialogInterface.OnClickListener listener = (dialog, i) -> {
switch (i) {
case DialogInterface.BUTTON_POSITIVE:
// use the old form instance and load the it's state from the descriptor
state.loadFromStateDescription(existing);
formEntry(platform.getFormDefId(state.getSession().getForm()), state.getFormRecord());
break;
case DialogInterface.BUTTON_NEGATIVE:
// delete the old incomplete form
FormRecordCleanupTask.wipeRecord(existing);
// fallthrough to new now that old record is gone
case DialogInterface.BUTTON_NEUTRAL:
// create a new form record and begin form entry
state.commitStub();
formEntry(platform.getFormDefId(state.getSession().getForm()), state.getFormRecord());
}
dismissAlertDialog();
};
d.setPositiveButton(Localization.get("option.yes"), listener);
d.setNegativeButton(Localization.get("app.workflow.incomplete.continue.option.delete"), listener);
d.setNeutralButton(Localization.get("option.no"), listener);
showAlertDialog(d);
}
public FloatingActionButton getIconButton(int whichButton) {
switch (whichButton) {
case DialogInterface.BUTTON_POSITIVE:
return mButtonBundlePositive.iconButton;
case DialogInterface.BUTTON_NEGATIVE:
return mButtonBundleNegative.iconButton;
case DialogInterface.BUTTON_NEUTRAL:
return mButtonBundleNeutral.iconButton;
default:
return null;
}
}
@Override public void onClick(DialogInterface dialog, int btnId) {
dialog.dismiss();
switch (btnId) {
case DialogInterface.BUTTON_POSITIVE:
sendDecision(MTMDecision.DECISION_ALWAYS);
break;
case DialogInterface.BUTTON_NEUTRAL:
sendDecision(MTMDecision.DECISION_ONCE);
break;
default:
sendDecision(MTMDecision.DECISION_ABORT);
}
}
/**
* Adapts the dialog's neutral button.
*/
private void adaptNeutralButton() {
if (neutralButton != null) {
neutralButton.setText(neutralButtonText != null ?
neutralButtonText.toString().toUpperCase(Locale.getDefault()) : null);
OnClickListenerWrapper onClickListener =
new OnClickListenerWrapper(neutralButtonListener, false, getDialog(),
DialogInterface.BUTTON_NEUTRAL);
neutralButton.setOnClickListener(onClickListener);
neutralButton.setVisibility(
!TextUtils.isEmpty(neutralButtonText) ? View.VISIBLE : View.GONE);
adaptButtonBarContainerVisibility();
}
}
public void onClick(DialogInterface dialog, int btnId) {
int decision;
dialog.dismiss();
switch (btnId) {
case DialogInterface.BUTTON_POSITIVE:
decision = MTMDecision.DECISION_ALWAYS;
break;
case DialogInterface.BUTTON_NEUTRAL:
decision = MTMDecision.DECISION_ONCE;
break;
default:
decision = MTMDecision.DECISION_ABORT;
}
sendDecision(decision);
}
/**
* Sets a click listener or a message to be sent when the button is clicked.
* You only need to pass one of {@code listener} or {@code msg}.
* @param whichButton Which button, can be one of
* {@link DialogInterface#BUTTON_POSITIVE},
* {@link DialogInterface#BUTTON_NEGATIVE}, or
* {@link DialogInterface#BUTTON_NEUTRAL}
* @param text The text to display in button.
* @param icon The icon to display in button.
* @param listener The {@link DialogInterface.OnClickListener} to use.
* @param msg The {@link Message} to be sent when clicked.
*/
public void setButton(int whichButton, CharSequence text, Drawable icon,
DialogInterface.OnClickListener listener, Message msg) {
if (msg == null && listener != null) {
msg = mHandler.obtainMessage(whichButton, listener);
}
switch (whichButton) {
case DialogInterface.BUTTON_POSITIVE:
mButtonBundlePositive.buttonText = text;
mButtonBundlePositive.buttonIcon = icon;
mButtonBundlePositive.buttonMessage = msg;
break;
case DialogInterface.BUTTON_NEGATIVE:
mButtonBundleNegative.buttonText = text;
mButtonBundleNegative.buttonIcon = icon;
mButtonBundleNegative.buttonMessage = msg;
break;
case DialogInterface.BUTTON_NEUTRAL:
mButtonBundleNeutral.buttonText = text;
mButtonBundleNeutral.buttonIcon = icon;
mButtonBundleNeutral.buttonMessage = msg;
break;
default:
throw new IllegalArgumentException("Button does not exist");
}
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DialogInterface.BUTTON_POSITIVE:
case DialogInterface.BUTTON_NEGATIVE:
case DialogInterface.BUTTON_NEUTRAL:
((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);
break;
case MSG_DISMISS_DIALOG:
((DialogInterface) msg.obj).dismiss();
}
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DialogInterface.BUTTON_POSITIVE:
case DialogInterface.BUTTON_NEGATIVE:
case DialogInterface.BUTTON_NEUTRAL:
((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);
break;
case MSG_DISMISS_DIALOG:
((DialogInterface) msg.obj).dismiss();
}
}
private void onImageLongClickExtraUrl(final String newUrl) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
newTab(newUrl, true);
break;
case DialogInterface.BUTTON_NEGATIVE:
getCurrentWebView().loadUrl(newUrl);
break;
case DialogInterface.BUTTON_NEUTRAL:
if (API > 8) {
Utils.downloadFile(mActivity, newUrl,
getCurrentWebView().getUserAgent(), "attachment", false);
}
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); // dialog
builder.setTitle(newUrl.replace(Constants.HTTP, ""))
.setMessage(getResources().getString(R.string.dialog_image))
.setPositiveButton(getResources().getString(R.string.action_new_tab),
dialogClickListener)
.setNegativeButton(getResources().getString(R.string.action_open),
dialogClickListener)
.setNeutralButton(getResources().getString(R.string.action_download),
dialogClickListener).show();
}
private void onLinkLongClick2(final String url) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
newTab(url, false);
break;
case DialogInterface.BUTTON_NEGATIVE:
getCurrentWebView().loadUrl(url);
break;
case DialogInterface.BUTTON_NEUTRAL:
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", url);
clipboard.setPrimaryClip(clip);
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity); // dialog
builder.setTitle(url)
.setMessage(getResources().getString(R.string.dialog_link))
.setPositiveButton(getResources().getString(R.string.action_new_tab),
dialogClickListener)
.setNegativeButton(getResources().getString(R.string.action_open),
dialogClickListener)
.setNeutralButton(getResources().getString(R.string.action_copy),
dialogClickListener).show();
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
super.onClick(dialog, which);
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
final boolean isEditing = !isIncomplete();
final SubtypeLocaleItem locale =
(SubtypeLocaleItem) mSubtypeLocaleSpinner.getSelectedItem();
final KeyboardLayoutSetItem layout =
(KeyboardLayoutSetItem) mKeyboardLayoutSetSpinner.getSelectedItem();
final InputMethodSubtype subtype =
AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
locale.mLocaleString, layout.mLayoutName);
setSubtype(subtype);
notifyChanged();
if (isEditing) {
mProxy.onSaveCustomInputStyle(this);
} else {
mProxy.onAddCustomInputStyle(this);
}
break;
case DialogInterface.BUTTON_NEUTRAL:
// Nothing to do
break;
case DialogInterface.BUTTON_NEGATIVE:
mProxy.onRemoveCustomInputStyle(this);
break;
}
}
public ResetEditPreferenceDialogFragCompat() {
mWhichClicked = DialogInterface.BUTTON_NEUTRAL;
}