@Test public void shouldPositiveListener_whenClickingPositiveButtonFromSupportFragment() { AlertDialog alertDialog = new AppSettingsDialog.Builder(spyFragment) .setTitle(TITLE) .setRationale(RATIONALE) .setPositiveButton(POSITIVE) .setNegativeButton(NEGATIVE) .setThemeResId(R.style.Theme_AppCompat) .build() .showDialog(positiveListener, negativeListener); Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); positive.performClick(); verify(positiveListener, times(1)) .onClick(any(DialogInterface.class), anyInt()); }
@Test public void shouldPositiveListener_whenClickingPositiveButtonFromActivity() { AlertDialog alertDialog = new AppSettingsDialog.Builder(spyActivity) .setTitle(TITLE) .setRationale(RATIONALE) .setPositiveButton(POSITIVE) .setNegativeButton(NEGATIVE) .setThemeResId(R.style.Theme_AppCompat) .build() .showDialog(positiveListener, negativeListener); Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); positive.performClick(); verify(positiveListener, times(1)) .onClick(any(DialogInterface.class), anyInt()); }
@Test public void shouldNegativeListener_whenClickingPositiveButtonFromSupportFragment() { AlertDialog alertDialog = new AppSettingsDialog.Builder(spyFragment) .setTitle(TITLE) .setRationale(RATIONALE) .setPositiveButton(POSITIVE) .setNegativeButton(NEGATIVE) .setThemeResId(R.style.Theme_AppCompat) .build() .showDialog(positiveListener, negativeListener); Button positive = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); positive.performClick(); verify(negativeListener, times(1)) .onClick(any(DialogInterface.class), anyInt()); }
@Test public void shouldNegativeListener_whenClickingPositiveButtonFromActivity() { AlertDialog alertDialog = new AppSettingsDialog.Builder(spyActivity) .setTitle(TITLE) .setRationale(RATIONALE) .setPositiveButton(POSITIVE) .setNegativeButton(NEGATIVE) .setThemeResId(R.style.Theme_AppCompat) .build() .showDialog(positiveListener, negativeListener); Button positive = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); positive.performClick(); verify(negativeListener, times(1)) .onClick(any(DialogInterface.class), anyInt()); }
@Override public void onSequenceCanceled(TapTarget lastTarget) { final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("Uh oh") .setMessage("You canceled the sequence") .setPositiveButton("Oops", null).show(); TapTargetView.showFor(dialog, TapTarget.forView(dialog.getButton(DialogInterface.BUTTON_POSITIVE), "Uh oh!", "You canceled the sequence at step " + lastTarget.id()) .cancelable(false) .tintTarget(false), new TapTargetView.Listener() { @Override public void onTargetClick(TapTargetView view) { super.onTargetClick(view); dialog.dismiss(); } }); } });
@Override public void onShow(DialogInterface dialogInterface) { dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (TextUtils.isEmpty(mCategoryTextField.getText())) { mCategoryTextField.setError(getString(R.string.category_empty)); } else { sendResult(Activity.RESULT_OK, false, mCategoryTextField.getText().toString()); dismiss(); } } }); if (isNew) { dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setVisibility(View.GONE); } } });
private void updateDialogOkButtonState() { boolean isDataValid = (mRules.size() > 0); for (int i = 0; i < mRules.size(); i++) { Rule rule = mRules.get(i); if (rule.reconnectDelay == -1 || (rule.repeatCount == -1 && i != mRules.size() - 1)) { isDataValid = false; break; } } mCurrentOkButtonState = isDataValid; mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(isDataValid); }
@Override public void onShow(DialogInterface dialogInterface) { dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setVisibility(View.GONE); dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (isNew) { for (String title : argCategories) { if (title.equals(s.toString())) { mCategoryTextField.setError(getString(R.string.category_exists)); dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); } else { dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); } } } }
private void enabledOrDisableButtons(String value) { if (getDialog() != null) { final AlertDialog dialog = ((AlertDialog) getDialog()); Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); if (button != null) { button.setEnabled( (!value.equals(mOriginalValue) || (value.isEmpty() && mModel.allowEmpty)) && (!value.isEmpty() || (value.isEmpty() && mModel.allowEmpty)) && (mRegExp == null || mRegExp.matcher(value).matches())); } } } }
private void enabledOrDisableButtons(String query) { if (getDialog() != null) { final AlertDialog dialog = ((AlertDialog) getDialog()); Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); if (button != null) { button.setEnabled(query.length() >= mBinding.reviewer.getThreshold()); } } } }
private void enabledOrDisableButtons(String query) { if (getDialog() != null) { final AlertDialog dialog = ((AlertDialog) getDialog()); Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); if (button != null) { button.setEnabled(query.length() >= mBinding.assignee.getThreshold()); } } } }
@Override @SuppressLint("RestrictedApi") public void setupDialog(Dialog dialog, int style) { super.setupDialog(dialog, style); dialog.setOnShowListener(dialog1 -> { Button button = ((AlertDialog) dialog1).getButton(AlertDialog.BUTTON_POSITIVE); button.setOnClickListener(v -> performAccountStateChanged()); }); }
if (dialog != null) { final EditText editText = dialog.findViewById(android.R.id.edit); Button positiveButton = dialog.getButton(Dialog.BUTTON_POSITIVE); if (editText != null && positiveButton != null) { positiveButton.setOnClickListener(new View.OnClickListener() {
@Override public void onResume() { super.onResume(); final AlertDialog dialog = (AlertDialog) getDialog(); if (dialog != null) { dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(v -> createTunnelAndDismiss()); setKeyboardVisible(true); } }
boolean enabledOrDisableButtons() { if (getDialog() != null) { final AlertDialog dialog = ((AlertDialog) getDialog()); Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); if (button != null) { boolean valid = isValidSelection() && isValidated(); button.setEnabled(valid); return valid; } } return false; }
@Override public void onStart() { super.onStart(); AlertDialog dialog = (AlertDialog) getDialog(); // http://stackoverflow.com/a/16972670/1048340 //noinspection ConstantConditions dialog.getWindow() .clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); // Do not dismiss the dialog when clicking the neutral button. Button neutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); if (neutralButton != null) { neutralButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { rootView.removeAllViews(); switch (dialogType) { case TYPE_CUSTOM: dialogType = TYPE_PRESETS; ((Button) v).setText(customButtonStringRes != 0 ? customButtonStringRes : R.string.cpv_custom); rootView.addView(createPresetsView()); break; case TYPE_PRESETS: dialogType = TYPE_CUSTOM; ((Button) v).setText(presetsButtonStringRes != 0 ? presetsButtonStringRes : R.string.cpv_presets); rootView.addView(createPickerView()); } } }); } }
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
@Override public Dialog onCreateDialog(@Nullable final Bundle savedInstanceState) { final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); alertDialogBuilder.setTitle(R.string.excluded_applications); final AppListDialogFragmentBinding binding = AppListDialogFragmentBinding.inflate(getActivity().getLayoutInflater(), null, false); binding.executePendingBindings(); alertDialogBuilder.setView(binding.getRoot()); alertDialogBuilder.setPositiveButton(R.string.set_exclusions, (dialog, which) -> setExclusionsAndDismiss()); alertDialogBuilder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); alertDialogBuilder.setNeutralButton(R.string.deselect_all, (dialog, which) -> { }); binding.setFragment(this); binding.setAppData(appData); loadData(); final AlertDialog dialog = alertDialogBuilder.create(); dialog.setOnShowListener(d -> dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setOnClickListener(view -> { for (final ApplicationData app : appData) app.setExcludedFromTunnel(false); })); return dialog; }