android实现 自定义键盘 keydemo
博客专区 > SuShine 的博客 > 博客详情
android实现 自定义键盘 keydemo
SuShine 发表于2年前
android实现 自定义键盘 keydemo
  • 发表于 2年前
  • 阅读 48
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

public class KeyboardUtil
{
    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, EditText edit) {
        this.act = act;
        this.ed = edit;
        hideSysIme(ed);
        k1 = new Keyboard(act, R.xml.qwerty);
        k2 = new Keyboard(act, 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;
    }
    
    private void hideSysIme(EditText editText) {
        if (android.os.Build.VERSION.SDK_INT <= 10) {//4.0以下 danielinbiti 
            editText.setInputType(InputType.TYPE_NULL);
        }
        else {
            this.act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setShowSoftInputOnFocus;
                setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(editText, false);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
}




demo地址  http://pan.baidu.com/s/1kT5N1JL

共有 人打赏支持
粉丝 118
博文 320
码字总数 79631
×
SuShine
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: