Android自定义软键盘

简介

今天在掘金上看了一篇文章,实现自定义软键盘,发现其实实现方式比较简单,不需要改动系统api,只是单纯的加载自己的键盘布局,隐藏系统弹出的键盘,实现数字错位,安全输入软键盘,记录一下实现过程用于总结

github地址: https://github.com/fushuangdage/CustomView

这里写图片描述

实现

<?xml version="1.0" encoding="utf-8"?>
<Keyboard
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="0px"
    android:keyHeight="9%p"
    android:keyWidth="25%p"
    android:verticalGap="0px"
    >
           
    <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="-5"
            android:isRepeatable="true"
            android:keyEdgeFlags="right"
            android:keyHeight="18%p"
            android:keyIcon="@drawable/icon_delete_32dp"/>
               
    </Row>
           
    <Row>
                       
        <Key
            android:codes="52"
            android:keyLabel="4"/>
                       
        <Key
            android:codes="53"
            android:keyLabel="5"/>
                       
        <Key
            android:codes="54"
            android:keyLabel="6"/>
                       
               
    </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="-4"
            android:keyEdgeFlags="right"
            android:keyHeight="18%p"
            android:keyLabel="确定"
            android:keyIcon="@drawable/icon_enter_32dp"/>
               
    </Row>
           
    <Row>
                       
        <Key
            android:codes="46"
            android:keyLabel="."/>
                     
        <Key
            android:codes="48"
            android:keyLabel="0"/>
                       
        <Key
            android:codes="-3"
            android:keyIcon="@drawable/icon_hide_keyboard"/>
               
    </Row>
</Keyboard>

首先编写关于自定义布局文件.键盘布局在xml定义

继承KeyboardView 画自己的键盘输入面板,可在ondraw方法中对默认的绘制放置覆盖绘制

package com.example.admin.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by fushuang on 2017/8/22.
 */

public class MykeyBoardView extends KeyboardView {


    private Context context;

    public MykeyBoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public MykeyBoardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Keyboard keyboard = getKeyboard();
        List<Keyboard.Key> keys=null;
        if (keyboard != null) {
            keys = keyboard.getKeys();
            for (Keyboard.Key key : keys) {
                if (key.codes[0]==-4) {
                   drawKeyBackground(R.drawable.bg_keyboardview_yes,canvas,key);
                    drawText(canvas,key);
                }
            }
        }
    }

    private void drawText(Canvas canvas, Keyboard.Key key) {
        Rect bounds = new Rect();
        Paint paint = new Paint();
        paint.setTextAlign(Paint.Align.CENTER);


        paint.setAntiAlias(true);

        paint.setColor(Color.WHITE);
        if (key.label != null) {
            String label = key.label.toString();

            Field field;

            if (label.length() > 1 && key.codes.length < 2) {
                int labelTextSize = 0;
                try {
                    field = KeyboardView.class.getDeclaredField("mLabelTextSize");
                    field.setAccessible(true);
                    labelTextSize = (int) field.get(this);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                paint.setTextSize(labelTextSize);
                paint.setTypeface(Typeface.DEFAULT_BOLD);
            } else {
                int keyTextSize = 0;
                try {
                    field = KeyboardView.class.getDeclaredField("mLabelTextSize");
                    field.setAccessible(true);
                    keyTextSize = (int) field.get(this);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                paint.setTextSize(keyTextSize);
                paint.setTypeface(Typeface.DEFAULT);
            }

            paint.getTextBounds(key.label.toString(), 0, key.label.toString()
                    .length(), bounds);
            canvas.drawText(key.label.toString(), key.x + (key.width / 2),
                    (key.y + key.height / 2) + bounds.height() / 2, paint);
        } else if (key.icon != null) {
            key.icon.setBounds(key.x + (key.width - key.icon.getIntrinsicWidth()) / 2, key.y + (key.height - key.icon.getIntrinsicHeight()) / 2,
                    key.x + (key.width - key.icon.getIntrinsicWidth()) / 2 + key.icon.getIntrinsicWidth(), key.y + (key.height - key.icon.getIntrinsicHeight()) / 2 + key.icon.getIntrinsicHeight());
            key.icon.draw(canvas);
        }

    }

    private void drawKeyBackground(int id, Canvas canvas, Keyboard.Key key) {
        Drawable drawable = context.getResources().getDrawable(id);
        int[] drawableState = key.getCurrentDrawableState();
        if (key.codes[0]!=0) {
            drawable.setState(drawableState);
        }
        drawable.setBounds(key.x,key.y, key.x+key.width,key.y+key.height);
        drawable.draw(canvas);
    }
}

KeyboardUtil 中主要实现当点输入文本框输入的隐藏软键盘,显示自定义键盘的逻辑

package com.example.admin.customview;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

/**
 * Created by fushuang on 2017/8/22.
 */

public class KeyboardUtil implements KeyboardView.OnKeyboardActionListener {

    private final MykeyBoardView mKeyBoardView;
    private  Keyboard mKeyboder;
    private EditText mEditText;
    private Activity mActivity;

    public KeyboardUtil(Activity mActivity) {
        this.mActivity = mActivity;
        mKeyboder = new Keyboard(mActivity,R.xml.keyboardnumber);
        mKeyBoardView = ((MykeyBoardView) mActivity.findViewById(R.id.keyboard_view));
    }

    public void attachTo(EditText editText){
        mEditText = editText;
        hideSystemSofeKeyboard(mActivity,editText);
        showSoftKeyBoard();
    }


    private boolean isNumber(String str) {
        String wordstr = "0123456789";
        return wordstr.contains(str);
    }

    private void showSoftKeyBoard() {
//        mKeyBoardView.setKeyboard(mKeyboder);
        List<Keyboard.Key> keys = mKeyboder.getKeys();
        List<Keyboard.Key> newkeyList = new ArrayList<Keyboard.Key>();
        for (Keyboard.Key key : keys) {
            if (key.label!=null && isNumber(key.label.toString())) {
                newkeyList.add(key);
            }
        }

        int count = newkeyList.size();
        LinkedList<KeyModel> temp=new LinkedList<KeyModel>();
        for (int i = 0; i < count; i++) {
            temp.add(new KeyModel(48 + i, i + ""));
        }

        Random random = new Random();
        for (int i = 0; i < count; i++) {
            int index = random.nextInt(count - i);
            KeyModel keyModel = temp.get(index);
            newkeyList.get(i).label=keyModel.getLable();
            newkeyList.get(i).codes[0]=keyModel.getCode();
            temp.remove(index);
        }

        mKeyBoardView.setKeyboard(mKeyboder);
        mKeyBoardView.setEnabled(true);
        mKeyBoardView.setPreviewEnabled(false);
        mKeyBoardView.setVisibility(View.VISIBLE);
        mKeyBoardView.setOnKeyboardActionListener(this);
    }


    /**
     * 隐藏系统键盘
     *
     * @param editText
     */
    public static void hideSystemSofeKeyboard(Context context, EditText editText) {
        int sdkInt = Build.VERSION.SDK_INT;
        if (sdkInt >= 11) {
            try {
                Class<EditText> cls = EditText.class;
                Method setShowSoftInputOnFocus;
                setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(editText, false);

            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            editText.setInputType(InputType.TYPE_NULL);
        }
        // 如果软键盘已经显示,则隐藏
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }


    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        Editable editable = mEditText.getText();
        int start = mEditText.getSelectionStart();
        if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
            if (editable != null && editable.length() > 0) {
                if (start > 0) {
                    editable.delete(start - 1, start);
                }
            }
        } else if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 隐藏键盘
            hideKeyboard();
//            if (mOnCancelClick != null) {
//                mOnCancelClick.onCancellClick();
//            }
        } else if (primaryCode == Keyboard.KEYCODE_DONE) {// 隐藏键盘
            hideKeyboard();
//            if (mOnOkClick != null) {
//                mOnOkClick.onOkClick();
//            }
        } else {
            editable.insert(start, Character.toString((char) primaryCode));
        }
    }

    private void hideKeyboard() {
        mKeyBoardView.setVisibility(View.GONE);
    }


    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {
        Toast.makeText(mActivity, "swipeLeft", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void swipeRight() {
        Toast.makeText(mActivity, "swipeRight", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void swipeDown() {
        Toast.makeText(mActivity, "swipeDown", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void swipeUp() {
        Toast.makeText(mActivity, "swipeUp", Toast.LENGTH_SHORT).show();

    }
}

只是照着网上代码自己实现了一遍,感觉只是有些方法不太常用.其余的跟自定义控件没啥区别

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容