zoukankan      html  css  js  c++  java
  • Android自定义键盘

    布局文件

     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>
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:keyWidth="25%p" android:horizontalGap="0px"
     4     android:verticalGap="0px" android:keyHeight="@dimen/key_height">
     5     <Row>
     6         <Key android:codes="49" android:keyLabel="1" />
     7         <Key android:codes="50" android:keyLabel="2" />
     8         <Key android:codes="51" android:keyLabel="3" />
     9         <Key android:codes="57419" android:keyEdgeFlags="right"
    10             android:keyIcon="@drawable/sym_keyboard_left" />
    11     </Row>
    12     <Row>
    13         <Key android:codes="52" android:keyLabel="4" />
    14         <Key android:codes="53" android:keyLabel="5" />
    15         <Key android:codes="54" android:keyLabel="6" />
    16         <Key android:codes="57421" android:keyEdgeFlags="right"
    17             android:keyIcon="@drawable/sym_keyboard_right" />
    18     </Row>
    19     <Row>
    20         <Key android:codes="55" android:keyLabel="7" />
    21         <Key android:codes="56" android:keyLabel="8" />
    22         <Key android:codes="57" android:keyLabel="9" />
    23         <Key android:codes="-3" android:keyHeight="100dip"
    24             android:keyEdgeFlags="right" android:isRepeatable="true"
    25             android:keyLabel="完成" />
    26     </Row>
    27     <Row>
    28         <Key android:codes="-2" android:keyLabel="ABC" />
    29         <Key android:codes="48" android:keyLabel="0" />
    30         <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" />
    31     </Row>
    32 </Keyboard>

    主要操作类

    package com.example.administrator.yunphone.UI;
    
    import java.util.List;
    
    import android.app.Activity;
    import android.content.Context;
    import android.inputmethodservice.Keyboard;
    import android.inputmethodservice.KeyboardView;
    import android.inputmethodservice.Keyboard.Key;
    import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
    import android.text.Editable;
    import android.view.View;
    import android.widget.EditText;
    
    import com.example.administrator.yunphone.R;
    
    public class KeyboardUtil {
        private Context ctx;
        private Activity act;
        private KeyboardView keyboardView;
        private Keyboard k1;// 字母键盘
        private Keyboard k2;// 数字键盘
        public boolean isnun = false;// 是否数据键盘
        public boolean isupper = false;// 是否大写
    
        private EditText ed;
    
        public KeyboardUtil(Activity act, Context ctx, EditText edit) {
            this.act = act;
            this.ctx = ctx;
            this.ed = edit;
            k1 = new Keyboard(ctx, R.xml.qwerty);
            k2 = new Keyboard(ctx, R.xml.symbols);
            keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
            keyboardView.setKeyboard(k1);
            keyboardView.setEnabled(true);
            keyboardView.setPreviewEnabled(true);
            keyboardView.setOnKeyboardActionListener(listener);
        }
    
        private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
            @Override
            public void swipeUp() {
            }
    
            @Override
            public void swipeRight() {
            }
    
            @Override
            public void swipeLeft() {
            }
    
            @Override
            public void swipeDown() {
            }
    
            @Override
            public void onText(CharSequence text) {
            }
    
            @Override
            public void onRelease(int primaryCode) {
            }
    
            @Override
            public void onPress(int primaryCode) {
            }
    
            @Override
            public void onKey(int primaryCode, int[] keyCodes) {
                Editable editable = ed.getText();
                int start = ed.getSelectionStart();
                if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
                    hideKeyboard();
                } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
                    if (editable != null && editable.length() > 0) {
                        if (start > 0) {
                            editable.delete(start - 1, start);
                        }
                    }
                } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
                    changeKey();
                    keyboardView.setKeyboard(k1);
    
                } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
                    if (isnun) {
                        isnun = false;
                        keyboardView.setKeyboard(k1);
                    } else {
                        isnun = true;
                        keyboardView.setKeyboard(k2);
                    }
                } else if (primaryCode == 57419) { // go left
                    if (start > 0) {
                        ed.setSelection(start - 1);
                    }
                } else if (primaryCode == 57421) { // go right
                    if (start < ed.length()) {
                        ed.setSelection(start + 1);
                    }
                } else {
                    editable.insert(start, Character.toString((char) primaryCode));
                }
            }
        };
        
        /**
         * 键盘大小写切换
         */
        private void changeKey() {
            List<Key> keylist = k1.getKeys();
            if (isupper) {//大写切换小写
                isupper = false;
                for(Key key:keylist){
                    if (key.label!=null && isword(key.label.toString())) {
                        key.label = key.label.toString().toLowerCase();
                        key.codes[0] = key.codes[0]+32;
                    }
                }
            } else {//小写切换大写
                isupper = true;
                for(Key key:keylist){
                    if (key.label!=null && isword(key.label.toString())) {
                        key.label = key.label.toString().toUpperCase();
                        key.codes[0] = key.codes[0]-32;
                    }
                }
            }
        }
    
        public void showKeyboard() {
            int visibility = keyboardView.getVisibility();
            if (visibility == View.GONE || visibility == View.INVISIBLE) {
                keyboardView.setVisibility(View.VISIBLE);
            }
        }
        
        public void hideKeyboard() {
            int visibility = keyboardView.getVisibility();
            if (visibility == View.VISIBLE) {
                keyboardView.setVisibility(View.INVISIBLE);
            }
        }
        
        private boolean isword(String str){
            String wordstr = "abcdefghijklmnopqrstuvwxyz";
            if (wordstr.indexOf(str.toLowerCase())>-1) {
                return true;
            }
            return false;
        }
    
    }

    使用

     public void setKeyBoard() {
            edit = (EditText)findViewById(R.id.edit);
            edit.setInputType(InputType.TYPE_NULL);
            edit.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    new KeyboardUtil(activity, context, edit).showKeyboard();
                    return false;
                }
            });
        }
  • 相关阅读:
    在SpringBoot或者Spring项目中实现最原始的分页功能
    element ui 弹出组件的遮罩层在弹出层的上面的解决方法
    vue中ref的使用(this.$refs获取为undefined)
    echarts的图表根据父容器大小的改变而改变(弹窗easy-ui的window窗口)
    vue项目使用history模式打包应该注意的地方
    echarts数据变了不重新渲染,以及重新渲染了前后数据会重叠渲染的问题
    element-ui的layout将24等分换为48等分
    vue中解决拖动和点击事件的冲突
    制作首页的显示列表。
    发布功能完成。
  • 原文地址:https://www.cnblogs.com/yoyohong/p/5680632.html
Copyright © 2011-2022 走看看