Tell me, how can you programmatically perform that when typing the maximum number of characters of the specified maxLenght in EditText, the keyboard is automatically removed? thank

EditText xKNPEditText = (EditText) findViewById(R.id.xKNP); avtoOffKeyPress(xKNPEditText); ...... public void avtoOffKeyPress(final EditText mEditText) { mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { Log.i("MyApp", "Длина= "+mEditText.getText().toString().length()); if(mEditText.getText().toString().length() == 5){ InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); } } @Override public void afterTextChanged(Editable editable) { } }); } 

Why doesn't it refer to the method ????

  • Describe what exactly you can’t do - how to hide the keyboard, how to calculate if you have reached the limit, or how to perform an action when the limit is reached. - Yuriy SPb

1 answer 1

In TextWatcher , in the onTextChanged(), method onTextChanged(), compare the current length of the entered text in edittext with the maximum possible length, as soon as they become equal, remove the keyboard so

 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 

Like that:

 mEditText = findViewById(R.id.my_edit_text); mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if(mEditText.getText().toString().length() == 5){ InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); } } @Override public void afterTextChanged(Editable editable) { } }); 

If you need to control several editText , you can create your own TextWatcher .

An example of a nested class, it is located in an activity class.

 private class GenericTextWatcher implements TextWatcher{ private View view; private GenericTextWatcher(View view) { this.view = view; } public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if( view instanceof EditText){ EditText editText = (EditText) view; if(editText.getText().toString().length() == 5){ InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); } } } public void afterTextChanged(Editable editable) {} } 

Using

 mEditText.addTextChangedListener(new GenericTextWatcher(mEditText)); 
  • Thanks, I'm still new. You can show by example how to associate an EditText object and a TextWatcher. Thank. - user281932
  • The task is this, there is EditText, maxLenght which has 5 so that when entering 5 characters the keyboard is removed and the text is read. - user281932
  • supplemented the answer. - Likhanov
  • You can see the code, why not refer to the method. - user281932
  • one
    Thanks, the code works, the error was in my program. - user281932