[英]Showing characters while typing in android?
I need to see characters while I am typing characters in keyboard.But i want to get result from keyboard. 我在键盘上键入字符时需要查看字符。但是我想从键盘上得到结果。
This is not used below this code : 此代码下未使用此代码:
@Override
public boolean dispatchKeyEvent(KeyEvent KEvent)
{
int keyaction = KEvent.getAction();
if(keyaction == KeyEvent.ACTION_DOWN)
{
int keycode = KEvent.getKeyCode();
int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState() );
char character = (char) keyunicode;
System.out.println("DEBUG MESSAGE KEY=" + character + " KEYCODE=" + keycode);
}
return super.dispatchKeyEvent(KEvent);
}
Approach:Basic Idea is to get the unicode character. 方法:基本思想是获取unicode字符。 What it is and how to work with it, take a look on the links below.
它是什么以及如何使用它,请查看下面的链接。 Following code shows three possibilities to get this unicode character from android.view.KeyEvent.
以下代码显示了从android.view.KeyEvent获取此unicode字符的三种可能性。 Try what works for you.
尝试对您有用的方法。
android.view.KeyEvent event = ...you got the event
// first option
int unicode = event.getUnicodeChar();
// second option with meta-state
int unicode = event.getUnicodeChar(event.getMetaState());
// third option over the KeyCharacterMap
KeyCharacterMap map = event.getKeyCharacterMap();
int unicode = map.get(keyCode, event.getMetaState());
// once you have the unicode as integer you can do this to get the char
char unicodeChar = Character.toChars(unicode)[0];
UPDATE: // for older versions of android 更新://适用于旧版本的android
Check this out in order to get the android.view.KeyCharacterMap: 进行检查以获取android.view.KeyCharacterMap:
KeyCharacterMap map = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
Or just try to get this map somehow over the static methods from the mentioned class. 或者只是尝试以某种方式通过上述类的静态方法获得此映射。
Good Programming! 好的编程! :-)
:-)
Some interesting links: 一些有趣的链接:
In order to get the text while typing into an android.widget.EditText you have to use the android.text.TextWatcher . 为了在输入android.widget.EditText时获取文本,您必须使用android.text.TextWatcher 。 Please try following:
请尝试以下操作:
Your initially question was to get the pressed key. 您最初的问题是获取按下的键。 Therefore use the passed java.lang.CharSequence and get the last char by:
因此,使用传递的java.lang.CharSequence并通过以下方式获取最后一个字符:
char lastKey = s.charAt(s.length() - 1);
This should work. 这应该工作。 Before using this check if not null or if the length is actually not 0. So some checks before are always a good idea :-)
在使用此检查之前,请检查是否不为null或长度实际上是否为0。因此,进行一些检查总是一个好主意:-)
Hope this time you get your result!! 希望这次您能得到结果! Good Programming!
好的编程!
PS I tried this right now in a dummy app with following code and it does work :-) PS我现在在一个带有以下代码的虚拟应用程序中尝试了它,它确实起作用:-)
this.editText = (EditText) this.findViewById(R.id.edit_text);
this.editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
if (s != null && s.length() > 0) {
final char lastKey = s.charAt(s.length()-1);
Toast.makeText(MainActivity.this, String.valueOf(lastKey),
Toast.LENGTH_SHORT).show();
}
}
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count,
final int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(final Editable s) {
// TODO Auto-generated method stub
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.