2015-01-20 38 views
0

我有一個自定義視圖。我從我的視圖中顯示系統鍵盤。我已經設置了我的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: 
+0

我會建議看看'TextView'和'EditText'如何處理它。 – CommonsWare 2015-01-20 20:50:16

+0

我同意,我真的不想要KeyUp和KeyDown;這正是我所發現的。我真正想要的是插入什麼字符。找到一種獲取這些信息的方法將是一大步。 – 2015-01-20 21:46:09

+0

使用TextWatcher。這會告訴你什麼時候插入或刪除一個字符。您需要與按鍵相關的EditText視圖。另一種(相當尷尬)的方式:在代碼中使用邏輯,如按KEY_CODE_DEL - 用戶想要刪除,按KEY_CODE_INSERT用戶正在插入/替換。 – cyanide 2015-01-20 22:50:35

回答

0

基於加布Sechan的評論,在這裏是部分答案。它確實得到了插入的文本。這是一大步。但是,到目前爲止,我無法弄清楚如何檢測刪除或按這種方式輸入按鍵。

像問題一樣,答案是通過Mono。 Java人會有稍微不同的方法名稱,但基本技術應該可以很好地工作。

我InputConnection類:

public class TextInputConnection: BaseInputConnection { 
    public TextInputConnection(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer): base(javaReference, transfer) { 
    } 
    public TextInputConnection(View view, bool fullEditor): base(view, fullEditor) { 
    } 
    public override bool CommitText(Java.Lang.ICharSequence text, int newCursorPosition) { 
     bool r = base.CommitText (text, newCursorPosition); 
     CommonDebug.LogLine ("Committing text", text.ToString(), newCursorPosition.ToString(), "returning", r.ToString()); // this is my console logging method, which logs to channel AAA. You'll want to substitute yours, or do whatever it is you want to do with the text. 
     return r; 
    } 
    public override bool PerformEditorAction(ImeAction actionCode) { 
     CommonDebug.LogLine ("PerformEditorAction", actionCode.ToString()); 
     return false; 
    } 
    } 

我認爲子類:

public override IInputConnection OnCreateInputConnection(EditorInfo outAttrs) { 
    TextInputConnection r = new TextInputConnection (this, true); 
    return r; 
    } 

當我在鍵盤上輸入 「狗」,我在控制檯看到以下內容:

[AAA] Committing text D 1 returning True 
[AAA] Committing text o 1 returning True 
[AAA] Committing text g 1 returning True 

這是相當大的進步,因爲我正在獲取實際插入的字符。一個「enter」按鍵會觸發PerformEditorAction方法,但ActionCode是「ImeNull」,所以它看起來不是很有幫助。而刪除按鍵不會觸發任何我能看到的東西。