G
G
Gariks2013-09-03 21:02:20
Java
Gariks, 2013-09-03 21:02:20

Handling keypresses in custom view?

I want to make a semblance of a wireless keyboard, and for this I need to process keyboard input.
It is important for me that there is no input text, no selection, copying, and other things typical for EditText, just a keyboard, preferably full screen.
developer.android.com/training/keyboard-input/inde... , the official source and googling did not give much, and now, in desperation, I am writing questions here.
1. How to call the soft keyboard programmatically (methods from the documentation do not work)?
2. How to handle keystrokes (considering different languages)?
3. Is it possible to stretch the keyboard to full screen?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Gariks, 2013-09-07
@Gariks

The method of scientific poke helps out. The keyboard can be invoked on the TextView and its children in this way

  public void showSoftKeyboard(View view) {

    if (view.requestFocus()) {
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

  }

In this case, view.requestFocus will work only if the element has flags view.setFocusable(true); or view.setFocusableInTouchMode(true); and InputMethodManager.SHOW_FORCED ensures that the keyboard view is correctly switched when the screen orientation changes.
Next in order.
 EditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); // в альбомном виде не показывается поле ввода и прочее
 EditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); // не показываются подсказки

^ these actions can also be performed from an XML file by setting the necessary attributes, but my element is generated programmatically.
Keystroke processing ExitText.addTextChangedListener and in the TextWatcher itself, the key detection procedure is implemented on afterTextChanged(Editable s) (if you change the Editable in the process, afterTextChanged will be called again with the changed data). TextWatcher catches ONLY text buttons, for example, it no longer processes backspace, it may be necessary to implement onKeyListener (not yet implemented).
There is also such an interesting thing as TextView.OnEditorActionListener, but according to experience, it only works when you press the “Done” button in the landscape orientation of the keyboard, and does not work on the “back” button that closes the keyboard - it turns out that hiding is not defined through it / keyboard close.

W
web_dev, 2013-09-03
@web_dev

1. Now I'm doing a project, the keyboard is called with a bang.

  private void showKeyboard() {

    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  }

или

  private void showKeyboard(EditText editText) {
    InputMethodManager imm = (InputMethodManager) AddPeriodActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question