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

我遇到了在4.4和5.0.1设备中无效的后退密钥或del密钥问题?当我按下软键盘的后退键时方法没有调用 .

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

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

回答(2)

2 years ago

正如我在这里找到https://developer.android.com/training/keyboard-input/commands.html

无法获得软键盘键事件

所以你应该使用 TextWatcher 来编辑文本并获得可用的char和删除的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 years ago

试试这个,这对我有用..

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





}

}