键盘控件KeyboardView的使用

(转载)http://blog.csdn.net/dreamInTheWorld/article/details/50917055

关于编写键盘,部分为了安全而APP自带键盘,比如手机银行;

效果为:


1 布局中增加 KeyboardView 控件;

2 设置 KeyboardView 监听键盘事件修改EditText


全部代码:
src/cn.demo.inputmethod/MainActivity.java
src/cn.demo.inputmethod/KeyboardUtil.java
res/layout/activity_main.xml
res/layout/key_preview_layout.xml
res/xml/qwerty.xml

activity_main.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:focusable="true"  
  6.     android:focusableInTouchMode="true"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <EditText  
  10.         android:id="@+id/editText"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:inputType="none"  
  14.         android:text="@string/hello_world"  
  15.         android:textSize="30sp" />  
  16.       
  17.     <RelativeLayout  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="match_parent" >  
  20.   
  21.         <android.inputmethodservice.KeyboardView  
  22.             android:id="@+id/keyboardView"  
  23.             android:layout_width="match_parent"  
  24.             android:layout_height="wrap_content"  
  25.             android:layout_alignParentBottom="true"  
  26.             android:focusable="true"  
  27.             android:focusableInTouchMode="true"  
  28.             android:keyPreviewLayout="@layout/key_preview_layout"  
  29.             android:keyBackground="@drawable/btn_keyboard_key"   
  30.             android:keyTextColor="@android:color/white"/>  
  31.     </RelativeLayout>  
  32.   
  33. </LinearLayout>  

MainActivity.java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.   
  3.     private EditText editText;  
  4.     private KeyboardView keyboardView;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.         editText = (EditText) findViewById(R.id.editText);  
  11.         keyboardView = (KeyboardView) findViewById(R.id.keyboardView);  
  12.         editText.setOnTouchListener(new View.OnTouchListener() {  
  13.             @Override  
  14.             public boolean onTouch(View v, MotionEvent event) {  
  15.                 if(!editText.hasFocus()){  
  16.                     int inputback = editText.getInputType();  
  17.                     editText.setInputType(InputType.TYPE_NULL);  
  18.                     new KeyboardUtil(keyboardView,editText).showKeyboard();  
  19.                     editText.setInputType(inputback);  
  20.                 }  
  21.                 return false;  
  22.             }  
  23.         });  
  24.     }  
  25. }  

KeyboardUtil.java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class KeyboardUtil {  
  2.   
  3.     private KeyboardView keyboardView;  
  4.     private EditText editText;  
  5.     private Keyboard k1;// 字母键盘  
  6.   
  7.     public KeyboardUtil(KeyboardView keyboardView1, EditText editText) {  
  8.         super();  
  9.         keyboardView = keyboardView1;  
  10.         this.editText = editText;  
  11.         keyboardView.setOnKeyboardActionListener(listener);  
  12.         k1 = new Keyboard(editText.getContext(), R.xml.qwerty);  
  13.         keyboardView.setKeyboard(k1);  
  14.         keyboardView.setEnabled(true);  
  15.         keyboardView.setPreviewEnabled(true);  
  16.     }  
  17.   
  18.     private OnKeyboardActionListener listener = new OnKeyboardActionListener() {  
  19.   
  20.         @Override  
  21.         public void swipeUp() {  
  22.         }  
  23.   
  24.         @Override  
  25.         public void swipeRight() {  
  26.         }  
  27.   
  28.         @Override  
  29.         public void swipeLeft() {  
  30.         }  
  31.   
  32.         @Override  
  33.         public void swipeDown() {  
  34.         }  
  35.   
  36.         @Override  
  37.         public void onText(CharSequence text) {  
  38.         }  
  39.   
  40.         @Override  
  41.         public void onRelease(int primaryCode) {  
  42.         }  
  43.   
  44.         @Override  
  45.         public void onPress(int primaryCode) {  
  46.         }  
  47.   
  48.         @Override  
  49.         public void onKey(int primaryCode, int[] keyCodes) {  
  50.             Editable editable = editText.getText();  
  51.             int start = editText.getSelectionStart();  
  52.             switch (primaryCode) {  
  53.             case Keyboard.KEYCODE_DELETE:  
  54.                 if (editable != null && editable.length() > 0) {  
  55.                     if (start > 0) {  
  56.                         editable.delete(start - 1, start);  
  57.                     }  
  58.                 }  
  59.                 break;  
  60.             case Keyboard.KEYCODE_CANCEL:  
  61.                 keyboardView.setVisibility(View.GONE);  
  62.                 break;  
  63.             default:  
  64.                 editable.insert(start, Character.toString((char) primaryCode));  
  65.                 break;  
  66.             }  
  67.         }  
  68.     };  
  69.   
  70.     // Activity中获取焦点时调用,显示出键盘  
  71.     public void showKeyboard() {  
  72.         int visibility = keyboardView.getVisibility();  
  73.         if (visibility == View.GONE || visibility == View.INVISIBLE) {  
  74.             keyboardView.setVisibility(View.VISIBLE);  
  75.         }  
  76.     }  
  77.   
  78. }  
其中需要建立键盘布局

使用xml文件建立

private Keyboard k1;
k1 = new Keyboard(editText.getContext(), R.xml.qwerty);
keyboardView.setKeyboard(k1);

其中有:

xml/qwerty.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Keyboard android:keyWidth="10.000002%p" android:keyHeight="@dimen/key_height"  
  3.     android:horizontalGap="0.0px" android:verticalGap="0.0px"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <Row>  
  6.         <Key android:codes="113" android:keyEdgeFlags="left"  
  7.             android:keyLabel="q" />  
  8.         <Key android:codes="119" android:keyLabel="w" />  
  9.         <Key android:codes="101" android:keyLabel="e" />  
  10.         <Key android:codes="114" android:keyLabel="r" />  
  11.         <Key android:codes="116" android:keyLabel="t" />  
  12.         <Key android:codes="121" android:keyLabel="y" />  
  13.         <Key android:codes="117" android:keyLabel="u" />  
  14.         <Key android:codes="105" android:keyLabel="i" />  
  15.         <Key android:codes="111" android:keyLabel="o" />  
  16.         <Key android:codes="112" android:keyEdgeFlags="right"  
  17.             android:keyLabel="p" />  
  18.     </Row>  
  19.     <Row>  
  20.         <Key android:horizontalGap="4.999995%p" android:codes="97"  
  21.             android:keyEdgeFlags="left" android:keyLabel="a" />  
  22.         <Key android:codes="115" android:keyLabel="s" />  
  23.         <Key android:codes="100" android:keyLabel="d" />  
  24.         <Key android:codes="102" android:keyLabel="f" />  
  25.         <Key android:codes="103" android:keyLabel="g" />  
  26.         <Key android:codes="104" android:keyLabel="h" />  
  27.         <Key android:codes="106" android:keyLabel="j" />  
  28.         <Key android:codes="107" android:keyLabel="k" />  
  29.         <Key android:codes="108" android:keyEdgeFlags="right"  
  30.             android:keyLabel="l" />  
  31.     </Row>  
  32.     <Row>  
  33.         <Key android:keyWidth="14.999998%p" android:codes="-1"  
  34.             android:keyEdgeFlags="left" android:isModifier="true"  
  35.             android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />  
  36.         <Key android:codes="122" android:keyLabel="z" />  
  37.         <Key android:codes="120" android:keyLabel="x" />  
  38.         <Key android:codes="99" android:keyLabel="c" />  
  39.         <Key android:codes="118" android:keyLabel="v" />  
  40.         <Key android:codes="98" android:keyLabel="b" />  
  41.         <Key android:codes="110" android:keyLabel="n" />  
  42.         <Key android:codes="109" android:keyLabel="m" />  
  43.         <Key android:keyWidth="14.999998%p" android:codes="-5"  
  44.             android:keyEdgeFlags="right" android:isRepeatable="true"  
  45.             android:keyIcon="@drawable/sym_keyboard_delete" />  
  46.     </Row>  
  47.     <Row android:rowEdgeFlags="bottom">  
  48.         <Key android:keyWidth="20.000004%p" android:codes="-2"  
  49.             android:keyLabel="12#" />  
  50.         <Key android:keyWidth="14.999998%p" android:codes="44"  
  51.             android:keyLabel="," />  
  52.         <Key android:keyWidth="29.999996%p" android:codes="32"  
  53.             android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />  
  54.         <Key android:keyWidth="14.999998%p" android:codes="46"  
  55.             android:keyLabel="." />  
  56.         <Key android:keyWidth="20.000004%p" android:codes="-3"  
  57.             android:keyEdgeFlags="right" android:keyLabel="完成" />  
  58.     </Row>  
  59. </Keyboard>  

补充下按键高度:<dimen name="key_height">50.0dip</dimen>

key_preview_layout.xml

作为提示的popWindow的布局编写:


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#ff8888ff"  
  6.     android:gravity="center"  
  7.     android:textColor="@android:color/white"  
  8.     android:textSize="40sp" />  

关于在MainActivity.java的editText.setOnTouchListener的监听中增加判断

if(!editText.hasFocus())

为了不重复设置,导致光标一直停留在文字末尾

但是如果初始时候,已经获取焦点了,就没有办法进入if里面导致键盘没有setVisible;

那么加点小技巧,比如一开始就不让EditText获取到焦点;

所以看到布局文件中有最外层布局LinearLayout:

android:focusable="true"

android:focusableInTouchMode="true"

参考 http://www.2cto.com/kf/201206/134893.html

那么最后放上项目源码的链接:

http://pan.baidu.com/s/1dEcflYh


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值