Good afternoon, I wrote a program that when you click on the onClickButton button, it displays a text field in which you can write text using the virtual keyboard, but when the text is written when you click on the OK button to apply it, nothing happens. You have to manually remove the keyboard and to apply the written text you need to press the onClickButton button again.

public void onClickButton(View view) { if(TextUtils.isEmpty(informationAboutFile)){//Ссли informationAboutFile содСрТит ноль mAutoComplete.setVisibility(AutoCompleteTextView.VISIBLE); } informationAboutFile = mAutoComplete.getText().toString();// ΠΈΠ½Ρ„Ρƒ с mAutoComplete ΠΏΠΎΠΌΠ΅Ρ‰Π°Π΅ΠΌ Π² informationAboutFile if (flag == true){ if (!TextUtils.isEmpty(informationAboutFile)) {//Ссли informationAboutFile Π½Π΅ пустой mAutoComplete.setVisibility(AutoCompleteTextView.INVISIBLE); try { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// НамСрСниС для запуска ΠΊΠ°ΠΌΠ΅Ρ€Ρ‹ intent.putExtra(MediaStore.EXTRA_OUTPUT, generateFileUri(TYPE_PHOTO)); startActivityForResult(intent, TAKE_PICTURE);// НамСрСниС для запуска ΠΊΠ°ΠΌΠ΅Ρ€Ρ‹ } catch (ActivityNotFoundException e) { // Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ сообщСниС ΠΎΠ± ошибкС Toast toast1 = Toast.makeText(getApplicationContext(), R.string.message2, Toast.LENGTH_LONG); toast1.setGravity(Gravity.CENTER, 0, 0); toast1.show(); } } } 

}

I try to do so, that is, assign the variable flag = true and, in the response, perform actions but there is no result.

  public boolean onKeyDown (int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_ENTER) { // ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ ΠΊΠ½ΠΎΠΏΠΊΠΈ DONE flag = true; return true; } return false; }/**/ 
  • So you need the keyboard not to close the button or make a button on the keyboard? - Flippy
  • By the way, why do you put the visibility on AutoCom....VISIBLE if you can just View.VISIBLE - Flippy
  • @ SergeyGrushin so the button on the keyboard is OK, you press it and nothing happens. I need that when it is pressed, the record is placed in a variable, everything is closed and further actions are done. - Varg Sieg
  • added to the answer, missed a trifle and nothing worked - pavlofff
  • one
    @pavlofff Thanks for the help, it all worked) - Varg Sieg

1 answer 1

The onKeyDown() works only on pressing the mechanical (hardware) buttons and does not apply to the buttons of the virtual keyboard.

You need to set the listening of the Enter button using OnEditorActionListener / The listener in the callback will return the actionId - in which form the Enter button is located. Available actionID state options

 editOK = (EditText) findViewById(R.id.edit); editOK.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { // ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ ΠΊΠ½ΠΎΠΏΠΊΠΈ DONE } return true; } return false; } 

The input field must always be set to one of the states inputType (except none ):

 <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text"/> 

If you need another text on the ENTER button (for example, OK instead of DONE), then for the input field, set your imeOption:

 editOK.setImeActionLabel("OK", EditorInfo.IME_ACTION_DONE); 

More about working with the keyboard

  • I do not quite understand how to get the result from onEditorAction, could you explain to me? - Varg Sieg
  • @VargSieg about what result you want to know - pavlofff
  • The result of pressing the key, I redid your example under mAutoComplete.setOnEditorActionListener (this), the onEditorAction boolean method should return true if the IDs are the same, but where it returns to call it in another method another question is it possible to use onKeyDown - Varg Sieg
  • write down your parameters to where it is written // handle pressing the DONE button . true it returns for the system, not for you - it means that the pressing processing has already been completed and the system does not need to handle pressing this button. Why use onKeyDown? - pavlofff
  • Completed the question, please see what could be the reason? - Varg Sieg