【问题标题】:Back key or Delete key of Soft Keyboard not working in 4.4 and 5.0?软键盘的返回键或删除键在 4.4 和 5.0 中不起作用?
【发布时间】:2015-05-15 16:22:48
【问题描述】:

我面临无法在 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;
    }

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

【问题讨论】:

标签: android layout android-edittext android-softkeyboard keyevent


【解决方案1】:

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

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





}

}

【讨论】:

    【解决方案2】:

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

    无法获取软键盘按键事件

    所以您应该使用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
                }
            });
    

    【讨论】:

    • 检查输入长度为 int input = s.length();输入为
    • 如果您想在 edittext 为空白时禁用注册按钮,那么我更新了我的答案看看 aftertextChange
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多