-1

我面临的问题是在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; 
    } 

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

+0

您在模拟器或手机上运行?因为一些联网设备有这个问题。请参阅https://code.google.com/p/android/issues/detail?id=42904 – 2015-03-13 08:46:47

+0

我正在真实设备上运行。 – TexGeek 2015-03-13 09:01:39

+0

我的要求有什么替代方案? – TexGeek 2015-03-13 09:02:20

回答

-1

因为我发现这里 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 
      } 
     }); 
+0

是的,afterTextChanged为我工作。 – TexGeek 2015-03-13 10:09:37

+0

检查输入的长度为int input = s.length();输入为<= 0 disbaled按钮 – TexGeek 2015-03-13 10:10:27

+0

如果你想禁用注册按钮,当edittext是空白的,然后我更新我的答案看下文文字变更 – IshRoid 2015-03-13 10:14:05

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





} 

}