2015-03-13 135 views
-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); 
    } 





} 

}