Android 软键盘的后退键或删除键在4.4和5.0中不起作用?

Android 软键盘的后退键或删除键在4.4和5.0中不起作用?,android,layout,android-edittext,android-softkeyboard,keyevent,Android,Layout,Android Edittext,Android Softkeyboard,Keyevent,我面临的问题是4.4和5.0.1设备中无法使用的back key或del key? 当我按下软键盘上的后退键时,下面的方法是不调用的 Username.setOnKeyListener(controller); Password.setOnKeyListener(controller); @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(event.getActi

我面临的问题是4.4和5.0.1设备中无法使用的back key或del key? 当我按下软键盘上的后退键时,下面的方法是不调用的

 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;
    }
有人建议我该怎么做吗? 如果用户名和密码中没有输入,我将禁用该按钮。 请建议我,如果您有其他解决方案,也请建议我。

正如我在这里找到的

无法获取软键盘按键事件

因此,您应该使用
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
            }
        });
yourTextView.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
//TODO自动生成的方法存根
}
@凌驾
公共无效后文本已更改(可编辑){
如果(yourTextView.getText().toString().length())
试试这个,这个对我有用


}

您在emulator或mobile上运行?因为某些nexus设备存在此问题。请参阅此,我在真实设备上运行。我的要求的替代方案是什么?检查输入长度为int input=s.length();当edittext为空时,如果您想禁用注册按钮,则我会更新我的答案look aftertextChange
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);
    }





}