软键盘的返回键或删除键在4.4和5.0中不起作用?
问题描述:
我面临的问题是在4.4和5.0.1设备中无法使用的后退键或del键? 当我按下softkeyboard的后退键时,方法不会调用。软键盘的返回键或删除键在4.4和5.0中不起作用?
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
这是不可能得到软键盘按键事件
所以,你应该使用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
}
});
答
试试这个,这个工作对我来说..
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://code.google.com/p/android/issues/detail?id=42904 – 2015-03-13 08:46:47
我正在真实设备上运行。 – TexGeek 2015-03-13 09:01:39
我的要求有什么替代方案? – TexGeek 2015-03-13 09:02:20