How can I convert a key code into a char or string?
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.开发者_C百科requestFocus();
break;
}
}
return false;
}
char unicodeChar = (char)event.getUnicodeChar();
Use event.getNumber()
.
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];
}
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()+"");
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))
Try this..
String s="";
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
EditText t=(EditText)v;
s=t.getText();
return false;
}
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 .
精彩评论