2015-01-20 46 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”,所以它看起来不是很有帮助。而删除按键不会触发任何我能看到的东西。