Back key or Delete key of Soft Keyboard not working in 4.4 and 5.0?

非 Y 不嫁゛ 提交于 2019-12-23 04:35:44

问题


I am facing the issue of back key or del key which is not working in 4.4 and 5.0.1 devices ? When I press the back key of softkeyboard below method is not calling.

 Username.setOnKeyListener(controller);
 Password.setOnKeyListener(controller);

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.KEYCODE_DEL){
            getActivity().setDisableLoginButton();
        }
        return false;
    }

Anyone suggest me what should I do ? I am disabling the button if there's no input in username & password. Please, suggest me also suggest me other solutions if you have.


回答1:


As i found here https://developer.android.com/training/keyboard-input/commands.html

It is not possible to get Soft Keyboard Key Events

So you should use TextWatcher for edit text and get available char and deleted char.

 yourTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                if(yourTextView.getText().toString().length()<=0){
                    //disabled button here
                    //It means your edittext is empty...
                }
                // TODO Auto-generated method stub
            }
        });



回答2:


Try this, this worked for me ..

public class InputConnectionProxyInput extends AppCompatEditText {
private static final String TAG = "InputConnectionProxyInput";

/**
 * Callback to handle the delete key press event.
 */
public interface SoftKeyDeleteCallback {
    void onDeleteKeyPressed(final EditText source);
}

private SoftKeyDeleteCallback mCallback;

public InputConnectionProxyInput(Context context) {
    super(context);

}


public InputConnectionProxyInput(Context context, AttributeSet attrs) {
    super(context, attrs);

}

public InputConnectionProxyInput(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

public void setSoftKeyDeleteCallback(SoftKeyDeleteCallback callback) {
    mCallback = callback;
}


@Override
protected void onSelectionChanged(int selStart, int selEnd) {
    /**
     * Doing this to avoid user selection
     */
    setSelection(this.length());
}

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return new ProxyConnectionWrapper(super.onCreateInputConnection(outAttrs), true);
}

/**
 * Creating a proxy class to handle the delete callback, in 4.3 and above we won't get the KeyEvent callback
 */
private class ProxyConnectionWrapper extends InputConnectionWrapper {

    public ProxyConnectionWrapper(InputConnection target, boolean mutable) {
        super(target, mutable);
    }


    @Override
    public boolean sendKeyEvent(KeyEvent event) {
        if ( event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_DEL && mCallback != null)
            mCallback.onDeleteKeyPressed(InputConnectionProxyInput.this);
        LogUtils.LOGD(TAG, "key code " + event.getAction());
        return super.sendKeyEvent(event);
    }





}

}



来源:https://stackoverflow.com/questions/29027953/back-key-or-delete-key-of-soft-keyboard-not-working-in-4-4-and-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!