我有一個自定義視圖。我從我的視圖中顯示系統鍵盤。我已經設置了我的keypressHandler,所以我得到了以下方法的回調。在Android上解釋系統按鍵
public bool OnKey(View v, Keycode keyCode, KeyEvent e)
[這都是通過單聲道,所以對於讀這個的人來說,方法名稱可能略有不同]。
我的問題是,我不明白如何將這些信息轉化爲對用戶想要做什麼的理解。我記錄了keyCode和keyEvent的一些信息;我已經在下面轉載了它。但我不明白如何獲取「用戶試圖插入字母'q'」或「用戶想要刪除字符」等信息。我可以爲它編寫自己的定製框架,但這不是一個好主意 - 有沒有辦法從系統中獲取這些信息?
我的代碼和一些調試器輸出:
public bool OnKey(View v, Keycode keyCode, KeyEvent e) {
KeyEventActions action = e.Action;
string actionString = action.ToString();
string text = keyCode.ToString();
KeyCharacterMap map = e.KeyCharacterMap;
MetaKeyStates meta = e.MetaState;
char label = map.GetDisplayLabel (keyCode);
CommonDebug.LogLine (actionString, "action detected", "code=", text, "meta=", meta.ToString(), "label=", label.ToString()); // custom console writing method.
}
[AAA] Down action detected code= H meta= 0 label= H
[AAA] Up action detected code= H meta= 0 label= H
[AAA] Down action detected code= ShiftLeft meta= 65 label= ��
[AAA] Down action detected code= H meta= 65 label= H
[AAA] Up action detected code= H meta= 65 label= H
[AAA] Up action detected code= ShiftLeft meta= 0 label= ��
[AAA] Down action detected code= ShiftLeft meta= 65 label= ��
[AAA] Down action detected code= D meta= 65 label= D
[AAA] Up action detected code= D meta= 65 label= D
對於它的價值,在iOS中我會用下面的方法來工作:
textField:shouldChangeCharactersInRange:replacementString:
我會建議看看'TextView'和'EditText'如何處理它。 – CommonsWare 2015-01-20 20:50:16
我同意,我真的不想要KeyUp和KeyDown;這正是我所發現的。我真正想要的是插入什麼字符。找到一種獲取這些信息的方法將是一大步。 – 2015-01-20 21:46:09
使用TextWatcher。這會告訴你什麼時候插入或刪除一個字符。您需要與按鍵相關的EditText視圖。另一種(相當尷尬)的方式:在代碼中使用邏輯,如按KEY_CODE_DEL - 用戶想要刪除,按KEY_CODE_INSERT用戶正在插入/替換。 – cyanide 2015-01-20 22:50:35