我遇到了在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;
}
有人建议我该怎么办? 如果用户名和输入中没有输入,我将禁用该按钮密码。 如果你有的话,请建议我也建议我使用其他解决方案。
答案 0 :(得分:0)
正如我在这里找到的那样 https://developer.android.com/training/keyboard-input/commands.html
无法获得软键盘键事件
因此,您应该使用TextWatcher
编辑文本并获取可用的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
}
});
答案 1 :(得分:0)
试试这个,这对我有用..
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);
}
}
}