如何将按键代码转换为字符或字符串?

发布于 2024-11-18 12:18:14 字数 799 浏览 3 评论 0原文

如何将keycode转换为charstring

这里是示例代码:

public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    //Log.d("EditText", "It's Working...!" + event.getAction());
    if (event.getAction() == 0) {
        switch (v.getId()) {
        case R.id.editText1:
            Log.d("EditText", "In editText1");
            if (text1.length() == 3)
            {
                text2.setText();
                text2.requestFocus();

            }
            break;

        case R.id.editText2:
            Log.d("EditText", "In editText2");
            if (text2.length() == 0)
                text1.requestFocus();
            break;
        }
    }

    return false;
}

How to convert the keycode into char or string??

Here is the example code:

public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    //Log.d("EditText", "It's Working...!" + event.getAction());
    if (event.getAction() == 0) {
        switch (v.getId()) {
        case R.id.editText1:
            Log.d("EditText", "In editText1");
            if (text1.length() == 3)
            {
                text2.setText();
                text2.requestFocus();

            }
            break;

        case R.id.editText2:
            Log.d("EditText", "In editText2");
            if (text2.length() == 0)
                text1.requestFocus();
            break;
        }
    }

    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夜夜流光相皎洁 2024-11-25 12:18:14
char unicodeChar = (char)event.getUnicodeChar();
char unicodeChar = (char)event.getUnicodeChar();
捂风挽笑 2024-11-25 12:18:14

如果您没有 KeyEvent 对象,您可以在键码上使用它:

public void onKey(int primaryCode, int[] keyCodes) {
     char c = Character.toChars(primaryCode)[0];
}

If you don't have a KeyEvent object you can use this on the keycode :

public void onKey(int primaryCode, int[] keyCodes) {
     char c = Character.toChars(primaryCode)[0];
}
并安 2024-11-25 12:18:14

Tod 的答案几乎已经完成,但是当您想使用此事件代码设置编辑文本的文本时,您应该添加一些东西:

sample_et.setText((char)event.getUnicodeChar()+"");

Tod answer is almost complete, but when you want to settext of an edittext with this eventcode you should to add a little thing:

sample_et.setText((char)event.getUnicodeChar()+"");
最笨的告白 2024-11-25 12:18:14

如果您想将一个按键从一个控件发送到另一个控件(例如,从 RecylerView 到其中的 EditText),您可以使用 这个

editText.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyCode, 0))
editText.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode, 0))

If you want to send a key from one control to another (for instance, from RecylerView to EditText inside it), you can use this:

editText.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyCode, 0))
editText.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode, 0))
傲性难收 2024-11-25 12:18:14

试试这个..

String s="";

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    EditText t=(EditText)v;
    s=t.getText();  
    return false;
}

Try this..

String s="";

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    EditText t=(EditText)v;
    s=t.getText();  
    return false;
}
花想c 2024-11-25 12:18:14

使用

String.fromCharCode();

String.fromCharCode(65,66,67) 返回 ABC

请参阅 https://developer.mozilla.org/ en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode

Use

String.fromCharCode();

String.fromCharCode(65,66,67) returns ABC.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文