简体   繁体   中英

Edittext cursor still blinks after closing the soft keyboard

Is an edittext cursor supposed to continue blinking after the soft keyboard is closed or is this a result of testing on an emulator and wouldn't happen on an actual device? -- as pointed out by the second post in this discussion

Update:

I know that the edittexts still have the cursor blinking because they're still in focus -- logged a message whenever edittext lost focus, but message was never logged when soft keyboard closed.

Update:

I've tried doing:

@Override
public void onBackPressed() {
    super.onBackPressed();
    getCurrentFocus().clearFocus();
}

So that every time the keyboard is closed, the EditText currently in focus loses that focus and onFocusChanged() is called. The problem is that onBackPressed() isn't called when the back button is pressed when the keyboard is up. I know this because I put a toast in onBackPressed(), and no toast shows when the back button is pressed whilst the keyboard is up.

First create a custom Edit text. Below is the example which has a call back when keyboard back is pressed to dismiss the keyboard

public class EdittextListner extends EditText {

private KeyImeChange keyImeChangeListener;

public EdittextListner(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setKeyImeChangeListener(KeyImeChange listener) {
    keyImeChangeListener = listener;
}

public interface KeyImeChange {
    public boolean onKeyIme(int keyCode, KeyEvent event);
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyImeChangeListener != null) {
        return keyImeChangeListener.onKeyIme(keyCode, event);
    }
    return false;
}

}

Secondly change your EditText to EdittextListner in you layout file.

Finally do the following

 mLastNameEditText.setKeyImeChangeListener(new EdittextListner.KeyImeChange() {
        @Override
        public boolean onKeyIme(int keyCode, KeyEvent event) {
            mLastNameEditText.clearFocus();
            return true;
        }
    });

This worked for me. Hope this helps

Edittext is a View which accept input from user, so it is not related with keyborad open or close, when user will click on edittext, that edittext will get focus and cursor will start to blink for taking input,

So you can do one thing as when you are closing keyboard at the same time you can also set visibility of cursor for that edittext so it will stop to blink,

For that you need to write below line when you hide keyboard.

    editTextObject.setCursorVisible(false);

This will stope cursor to blink.

As you said, the blinking cursor in the EditText is related to the EditText having focus, but showing or hiding the soft keyboard has no correlation to a View gaining or losing focus. Any View ( EditText or otherwise) can be focused independent of whether or not a soft keyboard is showing and there is nothing intrinsic to EditText that would make it behave any differently.

If you want an EditText to lose focus whenever the soft keyboard is hidden, you will need to implement this functionality yourself by listening for changes in the soft keyboard visibility and updating the EditText as a result.

The only way to know keyboard is disappeared is to override OnglobalLayout and check the height. Based on that event you can "setCursorVisible(false)" on your edit text

For more information, check this Link .

RelativeLayout mainLayout = findViewById(R.layout.main_layout); // You must use the layout root
InputMethodManager im = (InputMethodManager)  getSystemService(Service.INPUT_METHOD_SERVICE);

/*
Instantiate and pass a callback
*/
SoftKeyboard softKeyboard;
softKeyboard = new SoftKeyboard(mainLayout, im);
softKeyboard.setSoftKeyboardCallback(new SoftKeyboard.SoftKeyboardChanged()
{

@Override
public void onSoftKeyboardHide() 
{
    // Code here
  EditText.clearFocus();
}

@Override
public void onSoftKeyboardShow() 
{
    // Code here
}   
});

/*
Open or close the soft keyboard easily
*/
softKeyboard.openSoftKeyboard();
softKeyboard.closeSoftKeyboard();

/* Prevent memory leaks:
*/
@Override
public void onDestroy()
{
super.onDestroy();
softKeyboard.unRegisterSoftKeyboardCallback();
}

try this:

public class EditTextBackEvent extends EditText {

private EditTextImeBackListener mOnImeBack;

public EditTextBackEvent(Context context) {
    super(context);
}

public EditTextBackEvent(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString());
    }
    return super.dispatchKeyEvent(event);
}

public void setOnEditTextImeBackListener(EditTextImeBackListener listener) {
    mOnImeBack = listener;
}

public interface EditTextImeBackListener {
    void onImeBack(EditTextBackEvent ctrl, String text);
}
  }

in your layout:

<yourpackagename.EditTextBackEvent
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

and in your fragment:

edittext.setOnEditTextImeBackListener(new EditTextBackEvent.EditTextImeBackListener()
    {
        @Override
        public void onImeBack(EditTextBackEvent ctrl, String text)
        {
           edittext.clearfocus();
        }
    });

Try keeping a view in your layout which is focusable above your editText.

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:focusable="true"
    android:focusableInTouchMode="true" />

This should work as the blank focusable view should catch focus and not your edittext.

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