简体   繁体   中英

Android: soft keyboard Backspace on EditText not working

This is what happens: Activity A and B have an EditText and they both have IME_SEARCH . The input is done only via soft keyboard on a SAMSUNG tablet. On Activity AI can use the EditText without problems. The thing is that on Activity BI can't erase text with backspace after I hit 'space' or whenever I use a word from the suggestions. It behaves like there wasn't text there anymore. If I type new characters, I can erase them up to the space.

Important points:

  1. The View hierarchy that contains the EditTexts are identical
  2. The code that configures the IME_SEARCH processing (via setOnEditorActionListener ) is identical
  3. The TextWatcher of both are also identical
  4. In the Manifest, both activities are configures with
    android:configChanges="keyboardHidden|keyboard|orientation" android:windowSoftInputMode="stateAlwaysHidden|adjustUnspecified"

I set a breakpoint on the method beforeTextChanged of both TextWatcher . I inserted a 'space' and hit 'backspace'. On the Edittext of activity A, the method is triggered but on activity B's it is not triggered. I can't see the reason for this to happen since the properties of both Edittext are configured identically. I also tried removing the IME option but the behavior kept the same.

Does anyone know what could be happening?

EDIT 1:

searchTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    btnClear.setVisibility(View.GONE);
                } else{
                    btnClear.setVisibility(View.VISIBLE);
                }
            }
        });

searchTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    buildGrid();
                    return true;
                }
                return false;
            }
        });

EDIT 2: The layout hierarchy is as following.

<LinearLayout
    ... >

    <include layout="@layout/title_bar" />
    <RelativeLayout
        ...>
        <EditText
            ...>

The problem was that, for some reason, the Activity B was overriding dispatchKeyEvent() and always returning true . Removing it solved the problem.

I have similar problems that you are facing and I somehow managed to stumble on the solution. Apparently, I had setOnKeyListener to 'return true'. After I changed it to 'return false', the phone keyboard works perfect with backspace functioning properly once again on edittext. Hope this helps:

.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
  ...
  return false;
                }
        });

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