打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
自定义股票键盘

在开发股票App的时候可能会被要求使用股票键盘,前一阵从不炒股得人第一次听说了股票键盘,后来查阅了一下,其实就是自定义的键盘,奈何一只搜索股票键盘,难住了我,所以我写了一个股票键盘。

话不多说直接上马了:

首先要先写一个布局(就是在要显示键盘的类的布局里):

  1. <RelativeLayout  
  2.         android:layout_width="match_parent"  
  3.         android:layout_height="match_parent"  
  4.         android:layout_alignParentBottom="true"  
  5.         >  
  6.             <android.inputmethodservice.KeyboardView  
  7.                 android:id="@+id/hot_keyboard_view"  
  8.                 android:layout_width="fill_parent"  
  9.                 android:layout_height="wrap_content"  
  10.                 android:background="@android:color/black"  
  11.                 android:focusable="true"  
  12.                 android:focusableInTouchMode="true"  
  13.                 android:keyTextColor="@color/white"  
  14.                 android:keyPreviewLayout="@layout/key_preview_layout"  
  15.   
  16.                 />  
  17.     </RelativeLayout>  
其中KeyboardView键盘的控件。

接下来写一个键盘类:

  1. import android.app.Activity;  
  2. import android.content.Context;  
  3. import android.inputmethodservice.Keyboard;  
  4. import android.inputmethodservice.KeyboardView;  
  5. import android.text.Editable;  
  6. import android.view.View;  
  7. import android.widget.EditText;  
  8. import android.widget.ImageView;  
  9. import android.widget.LinearLayout;  
  10. import android.widget.RelativeLayout;  
  11.   
  12. import java.util.List;  
  13.   
  14. import cn.uniwa.uniwa.R;  
  15.   
  16. public class KeyBoardNumber {  
  17.     private KeyboardView keyboardView;  
  18.     private Keyboard k1;// 英文键盘  
  19.     private Keyboard k2;// 数字键盘  
  20.     private EditText ed;  
  21.     boolean b = true;  
  22.     Activity a;//接收传过来Activity  
  23.     LinearLayout ll;  
  24.     ImageView hotIvDel;  
  25.     LinearLayout hotLlResult;  
  26.     RelativeLayout hotRlNoFind;  
  27.   
  28.     public KeyBoardNumber(Activity act, Context ctx, EditText edit) {//Activity可以将使用到此键盘的类传过来,也可以删除不传  
  29.         this.ed = edit;  
  30.         a = act;  
  31.         k1 = new Keyboard(ctx, R.xml.letter);  
  32.         k2 = new Keyboard(ctx, R.xml.symbols);  
  33.         keyboardView = (KeyboardView) act.findViewById(R.id.hot_keyboard_view);  
  34.         keyboardView.setKeyboard(k2);  
  35.         //软键盘是否启用  
  36.         keyboardView.setEnabled(true);  
  37.         //按键提示是否启用  
  38.         keyboardView.setPreviewEnabled(false);  
  39.   
  40.         keyboardView.setOnKeyboardActionListener(listener);  
  41.         List<Keyboard.Key> keylist = k1.getKeys();  
  42.   
  43.         for (Keyboard.Key key : keylist) {  
  44.             if (key.label != null && isword(key.label.toString())) {  
  45.                 key.label = key.label.toString().toUpperCase();  
  46.                 key.codes[0] = key.codes[0] - 32;  
  47.             }  
  48.         }  
  49.     }  
  50.   
  51.     private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {  
  52.         @Override  
  53.         public void swipeUp() {  
  54.         }  
  55.   
  56.         @Override  
  57.         public void swipeRight() {  
  58.         }  
  59.   
  60.         @Override  
  61.         public void swipeLeft() {  
  62.         }  
  63.   
  64.         @Override  
  65.         public void swipeDown() {  
  66.         }  
  67.   
  68.         @Override  
  69.         public void onText(CharSequence text) {  
  70.         }  
  71.   
  72.         @Override  
  73.         public void onRelease(int primaryCode) {  
  74.         }  
  75.   
  76.         @Override  
  77.         public void onPress(int primaryCode) {  
  78.         }  
  79.   
  80.         @Override  
  81.         public void onKey(int primaryCode, int[] keyCodes) {//注意!设置的键盘按钮可以再此方法内设置  
  82.             ll = (LinearLayout) a.findViewById(R.id.hot_ll_text);  
  83.             Editable editable = ed.getText();  
  84.             int start = ed.getSelectionStart();  
  85.             if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成  
  86. //                Toast.makeText(KeyBoardNumber.this,"ok+"+editable,Toast.LENGTH_LONG).show();  
  87. //                hideKeyboard();  
  88.             } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退  
  89.                 if (editable != null && editable.length() > 0) {  
  90.                     if (start > 0) {  
  91.                         editable.delete(start - 1, start);  
  92. //                        hotIvDel.setVisibility(View.GONE);  
  93. //                        hotLlResult.setVisibility(View.GONE);  
  94. //                        ll.setVisibility(View.VISIBLE);  
  95. //                        hotRlNoFind.setVisibility(View.GONE);  
  96.                     }  
  97.                 }  
  98.             } else if (primaryCode == -15) {  
  99.                 EditText ed = (EditText) a.findViewById(R.id.hot_et_search);//这些方法用不到可以删除,这是从使用键盘的activity传来的id  
  100.                 hotLlResult = (LinearLayout) a.findViewById(R.id.hot_ll_result);  
  101.                 hotRlNoFind = (RelativeLayout) a.findViewById(R.id.hot_rl_noFind);  
  102.                 String text = ed.getText().toString();  
  103.                if (text.equals("")||text == null){  
  104.                    hotIvDel.setVisibility(View.GONE);  
  105.                    hotLlResult.setVisibility(View.GONE);  
  106.                    ll.setVisibility(View.VISIBLE);  
  107.                    hotRlNoFind.setVisibility(View.GONE);  
  108.                }  
  109.                 hideKeyboard();  
  110. //                ll.setVisibility(View.VISIBLE);  
  111. //                ed.setText("");  
  112.             } else if (primaryCode == 4896) {// 清空  
  113.                 editable.clear();  
  114.             } else if (primaryCode == 002) {  
  115.                 editable.insert(start, "002");  
  116.   
  117.             } else if (primaryCode == 300) {  
  118.                 editable.insert(start, "300");  
  119.   
  120.             } else if (primaryCode == 600) {  
  121.                 editable.insert(start, "600");  
  122.   
  123.             } else if (primaryCode == 601) {  
  124.                 editable.insert(start, "601");  
  125.   
  126.             } else if (primaryCode == 000) {  
  127.                 editable.insert(start, "000");  
  128.   
  129.             } else if (primaryCode == 115) {  
  130.                 editable.insert(start, "S");  
  131.             } else if (primaryCode == 8016) {  
  132. //              editable.insert(start, "16");  
  133.                 if (b) {  
  134.                     keyboardView.setKeyboard(k1);  
  135.                     b = false;  
  136.                 } else {  
  137.                     keyboardView.setKeyboard(k2);  
  138.                     b = true;  
  139.                 }  
  140.   
  141.             } else {  
  142.                 editable.insert(start, Character.toString((char) primaryCode));  
  143.             }  
  144.         }  
  145.     };  
  146.   
  147.     public boolean isKeyBoardShow() {  
  148.         int visibility = keyboardView.getVisibility();  
  149.         if (visibility == View.GONE || visibility == View.INVISIBLE) {  
  150.             return false;  
  151.         } else {  
  152.             return true;  
  153.         }  
  154.   
  155.     }  
  156.   
  157.     public void showKeyboard() {  
  158.         int visibility = keyboardView.getVisibility();  
  159.         if (visibility == View.GONE || visibility == View.INVISIBLE) {  
  160.             keyboardView.setVisibility(View.VISIBLE);  
  161.         }  
  162.     }  
  163.   
  164.     //隐藏键盘  
  165.     public void hideKeyboard() {  
  166.         int visibility = keyboardView.getVisibility();  
  167.         if (visibility == View.VISIBLE) {  
  168.             keyboardView.setVisibility(View.INVISIBLE);  
  169.         }  
  170.     }  
  171.   
  172.     private boolean isword(String str) {  
  173.         String wordstr = "abcdefghijklmnopqrstuvwxyz";  
  174.         if (wordstr.indexOf(str.toLowerCase()) > -1) {  
  175.             return true;  
  176.         }  
  177.         return false;  
  178.     }  

注意看我写的注释应该就能明白了


接下来是英文键盘:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:keyWidth="10%p" android:horizontalGap="0px"  
  4.     android:verticalGap="0px" android:keyHeight="8%p">  
  5.     <Row>  
  6.         <Key android:codes="113" android:keyEdgeFlags="left" android:keyLabel="Q"/>  
  7.         <Key android:codes="119" android:keyLabel="W"/>  
  8.         <Key android:codes="101" android:keyLabel="E"/>  
  9.         <Key android:codes="114" android:keyLabel="R"/>  
  10.         <Key android:codes="116" android:keyLabel="T"/>  
  11.         <Key android:codes="121" android:keyLabel="Y"/>  
  12.         <Key android:codes="117" android:keyLabel="U"/>  
  13.         <Key android:codes="105" android:keyLabel="I"/>  
  14.         <Key android:codes="111" android:keyLabel="O"/>  
  15.         <Key android:codes="112" android:keyEdgeFlags="right" android:keyLabel="P"/>  
  16.     </Row>  
  17.     <Row>  
  18.         <Key android:codes="97"   android:horizontalGap="4.999995%p" android:keyEdgeFlags="left" android:keyLabel="A"/>  
  19.         <Key android:codes="115" android:keyLabel="S"/>  
  20.         <Key android:codes="100" android:keyLabel="D"/>  
  21.         <Key android:codes="102" android:keyLabel="F"/>  
  22.         <Key android:codes="103" android:keyLabel="G"/>  
  23.         <Key android:codes="104" android:keyLabel="H"/>  
  24.         <Key android:codes="106" android:keyLabel="J"/>  
  25.         <Key android:codes="107" android:keyLabel="K"/>  
  26.         <Key android:codes="108" android:keyEdgeFlags="right" android:keyLabel="L"/>  
  27.   
  28.     </Row>  
  29.     <Row>  
  30.         <Key android:codes="122" android:horizontalGap="2%p" android:keyEdgeFlags="left" android:keyLabel="Z"/>  
  31.         <Key android:codes="120" android:keyLabel="X"/>  
  32.         <Key android:codes="99" android:keyLabel="C"/>  
  33.         <Key android:codes="118" android:keyLabel="V"/>  
  34.         <Key android:codes="98" android:keyLabel="B"/>  
  35.         <Key android:codes="110" android:keyLabel="N"/>  
  36.         <Key android:codes="109"  android:keyLabel="M"/>  
  37.         <Key android:codes="-5"  
  38.             android:isRepeatable="true"  
  39.             android:keyEdgeFlags="right"  
  40.             android:keyIcon="@drawable/sym_keyboard_delete"  
  41.             android:keyWidth= "25%p"/>  
  42.     </Row>  
  43.     <Row>  
  44.         <Key android:codes="8016"  
  45.             android:keyLabel="123"  
  46.             android:keyEdgeFlags="left"  
  47.             android:keyWidth= "25%p"/>  
  48.         <Key android:codes="4896"  
  49.             android:isRepeatable="true"  
  50.             android:keyWidth= "25%p"  
  51.             android:keyLabel="@string/key_clear" />  
  52.         <Key android:codes="-15"  
  53.             android:isRepeatable="true"  
  54.             android:keyWidth= "25%p"  
  55.             android:keyLabel="隐藏" />  
  56.         <Key android:codes="-3"  
  57.             android:isRepeatable="true"  
  58.             android:keyEdgeFlags="right"  
  59.             android:keyLabel="@string/key_ok"  
  60.             android:keyWidth= "25%p" />  
  61.   
  62.     </Row>  
  63.     </Keyboard>  

数字键盘:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:keyWidth="20%p" android:horizontalGap="0px"  
  4.     android:verticalGap="0px" android:keyHeight="8%p"  
  5.     >  
  6.     <Row>  
  7.         <Key android:codes="600" android:keyLabel="600" />  
  8.         <Key android:codes="49" android:keyLabel="1" />  
  9.         <Key android:codes="50" android:keyLabel="2" />  
  10.         <Key android:codes="51" android:keyLabel="3" />  
  11.         <Key android:codes="-5" android:isRepeatable="true"   
  12.             android:keyIcon="@drawable/sym_keyboard_delete" />  
  13.           
  14.     </Row>  
  15.     <Row>  
  16.         <Key android:codes="601" android:keyLabel="601" />  
  17.         <Key android:codes="52" android:keyLabel="4" />  
  18.         <Key android:codes="53" android:keyLabel="5" />  
  19.         <Key android:codes="54" android:keyLabel="6" />  
  20.         <Key android:codes="4896" android:keyLabel="@string/key_clear" />  
  21.           
  22.     </Row>  
  23.     <Row>  
  24.         <Key android:codes="000" android:keyLabel="000" />  
  25.         <Key android:codes="55" android:keyLabel="7" />  
  26.         <Key android:codes="56" android:keyLabel="8" />  
  27.         <Key android:codes="57" android:keyLabel="9" />  
  28.         <Key android:codes="-15" android:isRepeatable="true"  
  29.             android:keyLabel="隐藏" />  
  30.     </Row>  
  31.     <Row>  
  32.         <Key android:codes="8016" android:keyLabel="ABC" />  
  33.         <Key android:codes="002" android:keyLabel="002" />  
  34.         <Key android:codes="48" android:keyLabel="0" />         
  35.         <Key android:codes="300" android:keyLabel="300" />  
  36.         <Key android:codes="-3" android:isRepeatable="true"  
  37.             android:keyEdgeFlags="right"  
  38.             android:keyLabel="@string/key_ok"/>  
  39.     </Row>  
  40. </Keyboard>  

两个键盘内的功能键的codes都是重复的,可以进行数字英文进行切换

以上键盘设置完毕,只要在Activity中使用即可:

private KeyBoardNumber keyBoard;
keyBoard = new KeyBoardNumber(act, mContext, hotEtSearch);

 hotEtSearch.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View view, MotionEvent motionEvent) {                int inputback = hotEtSearch.getInputType();//                hotEtSearch.setInputType(InputType.TYPE_NULL);                keyBoard.showKeyboard();                hotEtSearch.setInputType(inputback);                return false;            }        });
这样就会显示键盘了;

但还是要注意,键盘布局的显示和隐藏;

如果有问题可以私聊我,会尽力帮忙。。


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
微信朋友圈被折叠?会自动化不存在的(下)
Android自定义键盘 – 预览视图约束到父布局
键盘Keyboard中的扫描码Scan Code 通码Make code 断码Break Code
【扫盲贴】教大家如何修正日文键盘的键位
一个51单片机的键盘扫描程序,算法简单有效
[620]使用Python实现一个按键精灵
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服