关于Android的EditText使用自定义键盘的问题
============问题描述============
小弟最近需要做一个自定义键盘,在网上搜了一些资料。使用android.inputmethodservice.KeyboardView这个东西来实现自定义键盘 遇到了几个小问题 求各位大神们来解答一下。
![](http://images0.cnblogs.com/blog/555442/201410/231642430437442.jpg)
![](http://images0.cnblogs.com/blog/555442/201410/231642439337069.png)
![](http://images0.cnblogs.com/blog/555442/201410/231642444968198.jpg)
keyboardView.setKeyboard(new Keyboard(this, R.xml.qwerty)); keyboardView.setEnabled(true); keyboardView.setPreviewEnabled(true); edt_text.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int inputType = edt_text.getInputType(); edt_text.setInputType(InputType.TYPE_NULL);// 让系统键盘不弹出 showKeyboard(); edt_text.setInputType(inputType); return false; } }); keyboardView.setOnKeyboardActionListener(new OnKeyboardActionListener() { public void onKey(int primaryCode, int[] keyCodes) { Editable editable = edt_text.getText(); int start = edt_text.getSelectionStart(); if (primaryCode == Keyboard.KEYCODE_CANCEL) { hideKeyboard(); } else if (primaryCode == Keyboard.KEYCODE_DELETE) { if (editable != null && editable.length() > 0) { editable.delete(start - 1, start); } } else { editable.insert(start, Character.toString((char) primaryCode)); } } public void swipeUp() { // TODO Auto-generated method stub } public void swipeRight() { // TODO Auto-generated method stub } public void swipeLeft() { // TODO Auto-generated method stub } public void swipeDown() { // TODO Auto-generated method stub } public void onText(CharSequence text) { // TODO Auto-generated method stub } public void onRelease(int primaryCode) { // TODO Auto-generated method stub } public void onPress(int primaryCode) { // TODO Auto-generated method stub } }); private void showKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.GONE || visibility == View.INVISIBLE) { keyboardView.setVisibility(View.VISIBLE); System.out.println("showKeyboard"); } } private void hideKeyboard() { int visibility = keyboardView.getVisibility(); if (visibility == View.VISIBLE) { keyboardView.setVisibility(View.INVISIBLE); System.out.println("hideKeyboard"); } }
Activity布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/edt_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/keyboard" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <android.inputmethodservice.KeyboardView android:id="@+id/keyboard_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:focusable="true" android:focusableInTouchMode="true" android:visibility="gone" /> </RelativeLayout> </LinearLayout>
自定义键盘布局:
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="33%p" android:horizontalGap="0px" android:verticalGap="0px" android:keyHeight="46dip"> <Row> <Key android:codes="49" android:keyLabel="1" /> <Key android:codes="50" android:keyLabel="2" /> <Key android:codes="51" android:keyLabel="3" /> <!-- <Key android:codes="57419" --> <!-- android:keyEdgeFlags="right" --> <!-- android:keyIcon="@drawable/sym_keyboard_left" /> --> </Row> <Row> <Key android:codes="52" android:keyLabel="4" /> <Key android:codes="53" android:keyLabel="5" /> <Key android:codes="54" android:keyLabel="6" /> <!-- <Key android:codes="57421" --> <!-- android:keyEdgeFlags="right" --> <!-- android:keyIcon="@drawable/sym_keyboard_right" /> --> </Row> <Row> <Key android:codes="55" android:keyLabel="7" /> <Key android:codes="56" android:keyLabel="8" /> <Key android:codes="57" android:keyLabel="9" /> <!-- <Key android:codes="-3" --> <!-- android:keyHeight="92dip" --> <!-- android:keyEdgeFlags="right" --> <!-- android:isRepeatable="true" 两列 --> <!-- android:keyLabel="完成" /> --> </Row> <Row> <Key android:codes="42" android:keyLabel="*" /> <Key android:codes="48" android:keyLabel="0" /> <Key android:codes="35" android:keyLabel="#" /> <!-- <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" /> --> </Row> </Keyboard>
============解决方案1============
高手不会来这里的。你没看到一堆问号?提问帖的恢复寥寥无几,有资源了就全来了。。。 哈哈 还是百度好
============解决方案2============
http://download.csdn.net/detail/yuanjiangxujin/5641095