简体   繁体   中英

How can I stop further text input in EditText on a button click and keep focus?

I am using a Button to check text entered into an EditText. After the button click event I don't want to allow any more text to be entered.

I previously used setEnabled(false) but the problem with this is the software keyboard is then closed, which means the user has to reopen it which is very inconvenient (the process in a loop is essentially: text entry --> button1 (stop text entry) --> button 2 --> text entry etc...). I tried to avoid this by adding in the code:

setEnabled(false);
InputMethodManager imm = (InputMethodManager)  getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

But, I notice this creates a short flash sometimes as the keyboard is quickly minimized and then restored again.

There must be another way to stop additional text alteration without losing the focus of the EditText?

Given the situation you are describing, the only thing I can think of would be to store the value and restore it at the end of your operation (as opposed to disabling the text box). It's not a very correct approach, but it should work...

You could use an InputFilter :

int currentTextSize = editText.getText.toString().length();
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(currentTextSize);
editText.setFilters(FilterArray);

and to remove it :

editText.setFilters(new InputFilter[] {});

You could of course declare those filters as static in your class if they are to be used often... This might not be the best way to achieve this but it's the first I thought of ;)

I managed to implement a solution to this that I was happy with, although its not a particularly elegant solution. For anyone who might stumble across this in the future:

I created the following EditText in my xml which provides an EditText to temporarily receive focus and means the keyboard remains open. Note the width and height are set to 0px so it is a hidden object.

    <EditText
        android:id="@+id/switcher"
        android:layout_width="0px"
        android:maxLength="1"
        android:layout_height="0px"
        android:textSize="10sp"/>

Then in my button click event I added the following code:

    EditText attempt= (EditText) findViewById(R.id.attempt);
    EditText switcher= (EditText) findViewById(R.id.switcher);
    switcher.requestFocus();
    attempt.setFocusable(false);

This moves the focus to hidden EditText and then stops the attempt text object from being able to be edited again. It can then be removed on another action with:

    attempt.setFocusable(true);
    attempt.setFocusableInTouchMode(true);
    attempt.requestFocus();

Perhaps switcher.setFocusable should be changed between the two (but in the opposite way)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM