我似乎找不到这样做的方法:我想从键盘存储用户的单个字符作为用户输入的一部分,只要他们输入它。我不需要文本字段或提交按钮。提示用户从键盘键入一个字符并将其存储为字符或字符串。
任何链接寻求帮助?
在没有edittext的情况下在android上捕获键盘事件是不好的做法,现有的解决方案在某些设备上不起作用,考虑创建你的键盘小部件
http://developer.android.com/reference/android/inputmethodservice/Keyboard.html
KeyboardView mKeyboardView;
Keyboard mNumberKeyboard;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNumberKeyboard = new Keyboard(this, R.xml.keyboard);
mKeyboardView=(KeyboardView) this.findViewById(R.id.num_keyboard);
mKeyboardView.setKeyboard(mNumberKeyboard);
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setPreviewEnabled(false);
mKeyboardView.setOnKeyboardActionListener(new OnKeyboardActionListener(){
@Override
public void onKey(int arg0, int[] arg1) {
SharedPreferences sp = getSharedPreferences("MySharedPreferences", 0);
SharedPreferences.Editor edit = sp.edit();
edit.putInt("key", arg0);
edit.commit();
}
@Override public void onPress(int primaryCode) {}
@Override public void onRelease(int primaryCode) {}
@Override public void onText(CharSequence text) {}
@Override public void swipeDown() {}
@Override public void swipeLeft() {}
@Override public void swipeRight() {}
@Override public void swipeUp() {}
});
}