How can I convert a key code into a char or string?

戏子无情 提交于 2019-11-27 07:35:48

问题


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;
}

回答1:


char unicodeChar = (char)event.getUnicodeChar();



回答2:


Use event.getNumber().




回答3:


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];
}



回答4:


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()+"");



回答5:


Try this..

String s="";

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



回答6:


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 .



来源:https://stackoverflow.com/questions/6571081/how-can-i-convert-a-key-code-into-a-char-or-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!