软键盘的返回键或删除键在 4.4 和 5.0 中不起作用?

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

我遇到了返回键或删除键在 4.4 和 5.0.1 设备上不起作用的问题? 当我按下软键盘的后退键时,下面的方法没有调用。

 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;
    }

有人建议我该怎么做吗? 如果没有输入用户名和密码,我将禁用该按钮。 请建议我,如果你有其他解决方案,请建议我。

正如我在这里发现的 https://developer.android.com/training/keyboard-input/commands.html

It is not possible to get Soft Keyboard Key Events

因此您应该使用 TextWatcher 来编辑文本并获取可用字符和已删除字符。

 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
            }
        });

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);
    }





}

}