Java Code Examples for android.app.AlertDialog#setOnKeyListener()

The following examples show how to use android.app.AlertDialog#setOnKeyListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VersionUpdateDialog.java    From Yuan-WanAndroid with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_version_update, null);
    AlertDialog dialog = new AlertDialog.Builder(getActivity())
            .setView(view)
            .setCancelable(false)
            .create();
    dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
    dialog.setOnKeyListener((dialog1, keyCode, event) -> keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0);
    TextView updateLater = view.findViewById(R.id.updateLaterTv);
    TextView updateNow = view.findViewById(R.id.updateNowTv);
    TextView content = view.findViewById(R.id.versionContentTv);
    content.setText(mContentText);
    updateLater.setOnClickListener(v -> dialog.dismiss());
    updateNow.setOnClickListener(v -> {
        dialog.dismiss();
        RxBus.getInstance().post(new UpdateEvent());
    });
    return dialog;
}
 
Example 2
Source File: KeyComboPreference.java    From talkback with Apache License 2.0 6 votes vote down vote up
@Override
protected void showDialog(Bundle state) {
  super.showDialog(state);
  AlertDialog alertDialog = (AlertDialog) getDialog();
  if (alertDialog == null) {
    return;
  }

  if (getContext().getPackageManager().hasSystemFeature("android.hardware.touchscreen")) {
    /* Disable focus for buttons to prevent them being highlighted when keys are pressed. */
    alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFocusable(false);
    alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setFocusable(false);
  }
  alertDialog.setOnKeyListener(this);
  alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(R.string.save);
  alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setText(android.R.string.cancel);
}
 
Example 3
Source File: KeyboardShortcutDialogPreference.java    From talkback with Apache License 2.0 6 votes vote down vote up
@Override
protected void showDialog(Bundle state) {
  super.showDialog(state);
  AlertDialog alertDialog = (AlertDialog) getDialog();
  if (alertDialog == null) {
    return;
  }

  View clear = alertDialog.findViewById(R.id.clear);
  clear.setOnClickListener(clearButtonClickListener);
  alertDialog
      .getButton(DialogInterface.BUTTON_POSITIVE)
      .setOnClickListener(okButtonClickListener);
  alertDialog.setOnKeyListener(this);

  Button okButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
  okButton.setFocusableInTouchMode(true);
  okButton.requestFocus();

  setKeyEventSource(getKeyEventSourceForCurrentKeyComboModel());
}
 
Example 4
Source File: DirectoryChooserDialog.java    From fastnfitness with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void chooseDirectory(String dir) {
    File dirFile = new File(dir);
    if (!dirFile.exists() || !dirFile.isDirectory()) {
        dir = m_sdcardDirectory;
    }

    try {
        dir = new File(dir).getCanonicalPath();
    } catch (IOException ioe) {
        return;
    }

    m_dir = dir;
    m_subdirs = getDirectories(dir);

    class DirectoryOnClickListener implements DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialog, int item) {
            // Navigate into the sub-directory
            m_dir += "/" + ((AlertDialog) dialog).getListView().getAdapter().getItem(item);
            updateDirectory();
        }
    }

    AlertDialog.Builder dialogBuilder =
        createDirectoryChooserDialog(dir, m_subdirs, new DirectoryOnClickListener());

    dialogBuilder.setPositiveButton("OK", (dialog, which) -> {
        // Current directory chosen
        if (m_chosenDirectoryListener != null) {
            // Call registered listener supplied with the chosen directory
            m_chosenDirectoryListener.onChosenDir(m_dir);
        }
    }).setNegativeButton("Cancel", null);

    final AlertDialog dirsDialog = dialogBuilder.create();

    dirsDialog.setOnKeyListener((dialog, keyCode, event) -> {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            // Back button pressed
            if (m_dir.equals(m_sdcardDirectory)) {
                // The very top level directory, do nothing
                return false;
            } else {
                // Navigate back to an upper directory
                m_dir = new File(m_dir).getParent();
                updateDirectory();
            }

            return true;
        } else {
            return false;
        }
    });

    // Show directory chooser dialog
    dirsDialog.show();
}