继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

重写Keyboard

慕哥9229398
关注TA
已关注
手记 1245
粉丝 199
获赞 913

各键对应的编码

webp

绘制步骤

1、写keyboard的xml文件
    <?xml version="1.0" encoding="utf-8"?>
    <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
        android:horizontalGap="12dp"
        android:verticalGap="14dp"
        android:keyWidth="33.33333%p"
        android:keyHeight="8%p">

        <Row>
            <Key
                android:codes="49"
                android:keyLabel="1"></Key>
            <Key
                android:codes="50"
                android:keyLabel="2"></Key>
            <Key
                android:codes="51"
              android:keyLabel="3"></Key>
            <Key
                android:codes="52"
                android:keyLabel="4"></Key>
            <Key
                android:codes="53"
                android:keyLabel="5"></Key>
        </Row>

        <Row>
              <Key
                android:codes="54"
                android:keyLabel="6"></Key>
            <Key
                android:codes="55"
                android:keyLabel="7"></Key>
            <Key
                android:codes="56"
                android:keyLabel="8"></Key>
            <Key
                android:codes="57"
                android:keyLabel="9"></Key>
            <Key
                android:codes="48"
                android:keyLabel="0"></Key>
            </Row>

        <Row>
            <Key
                android:codes="43"
                android:keyLabel="+"></Key>
            <Key
                android:codes="45"
                android:keyLabel="-"></Key>
            <Key
                android:codes="46"
                android:keyLabel="."></Key>
            <!--KEYCODE_CANCEL-->
            <Key
                android:codes="-3"
                android:keyLabel="C"></Key>
            <!--KEYCODE_DELETE-->
            <Key
                android:codes="-5"
                android:isRepeatable="false"
                android:keyIcon="@drawable/ic_calculator_delete"/>
        </Row>
    </Keyboard>
2、重写 KeyboardView
    public class NumKeyView extends KeyboardView implements KeyboardView.OnKeyboardActionListener {    //用于区分左下角空白按键,(要与xml里设置的数值相同)
    private int KEYCODE_EMPTY = -10;    private Drawable mDeleteKeyDrawable;   //删除按键背景图片
    private int mKryboardBackgroud;    private Drawable mKryDrawable;   //按键背景
    private int mPaddingLeft;    private int mPaddingRight;    private int mPaddingTop;    private int mPaddingBottom;    public NumKeyView(Context context, AttributeSet attrs) {        super(context, attrs);
        init(context, attrs, 0);
    }    public NumKeyView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NumKeyView);
        mKryDrawable = ta.getDrawable(R.styleable.NumKeyView_keyBackgBackground);
        mDeleteKeyDrawable = ta.getDrawable(R.styleable.NumKeyView_deleteDrawable); //删除按键颜色
        mKryboardBackgroud = ta.getColor(R.styleable.NumKeyView_keyboardBackgBackground, Color.WHITE); //keyboard背景颜色
        mPaddingLeft = (int) ta.getDimension(R.styleable.NumKeyView_leftPadding, 0);
        mPaddingRight = (int) ta.getDimension(R.styleable.NumKeyView_rightPadding, 0);
        mPaddingTop = (int) ta.getDimension(R.styleable.NumKeyView_topPadding, 0);
        mPaddingBottom = (int) ta.getDimension(R.styleable.NumKeyView_bottomPadding, 0);
        ta.recycle();        //获取xml中的按键布局
        Keyboard keyboard = new Keyboard(context, R.xml.numkeyview);
        setKeyboard(keyboard);

        setEnabled(true);
        setPreviewEnabled(false);
        setOnKeyboardActionListener(this);
    }    @Override
    public void onDraw(Canvas canvas) {        super.onDraw(canvas);
        canvas.drawColor(mKryboardBackgroud);

        drawKeyboardBorder(canvas);

        Keyboard keyboard = getKeyboard();        if (keyboard == null) return;
        List<Keyboard.Key> keys = keyboard.getKeys();        if (keys != null && keys.size() > 0) {
            Paint paint = new Paint();
            paint.setTextAlign(Paint.Align.CENTER);
            Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
            paint.setTypeface(font);
            paint.setAntiAlias(true);            for (Keyboard.Key key : keys) {                if (key.codes[0] == Keyboard.KEYCODE_DELETE) {                    //绘制删除键背景
                    drawKeyBackGround(key, canvas);                    //绘制按键图片
                    drawkeyDelete(key, canvas);
                } else {
                    drawKeyBackGround(key, canvas);
                }                if (key.label != null) {                    if (key.codes[0] == Keyboard.KEYCODE_DELETE) {

                    } else {
                        paint.setColor(getContext().getResources().getColor(R.color.c000000));
                        paint.setTextSize(sp2px(25));
                    }
                    Rect rect = new Rect(key.x, key.y, key.x + key.width, key.y + key.height);
                    Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();                    int baseline = (rect.bottom + rect.top - fontMetrics.bottom - fontMetrics.top) / 2;                    // 下面这行是实现水平居中,drawText对应改为传入targetRect.centerX()
                    paint.setTextAlign(Paint.Align.CENTER);
                    canvas.drawText(key.label.toString(), rect.centerX(), baseline, paint);
                }
            }
        }
    }    //绘制边框
    private void drawKeyboardBorder(Canvas canvas) {

    }    //数字键
    private void drawKeyBackGround(Keyboard.Key key, Canvas canvas) {
        mKryDrawable.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
        mKryDrawable.draw(canvas);
    }    //删除键
    private void drawkeyDelete(Keyboard.Key key, Canvas canvas) {        int drawWidth = key.width;        int drawHeight = key.height;
        drawWidth = drawWidth / 2;
        drawHeight = drawHeight / 2;        int widthInterval = (key.width - drawWidth) / 2;        int heightInterval = (key.height - drawHeight) / 2;

        mDeleteKeyDrawable.setBounds(key.x + widthInterval, key.y + heightInterval,
                key.x + widthInterval + drawWidth, key.y + heightInterval + drawHeight);
        mDeleteKeyDrawable.draw(canvas);
    }    //回调接口
    public interface OnKeyPressListener {        //添加数据回调
        void onInertKey(String text);        //删除数据回调
        void onDeleteKey();        void onClearKey();
    }    private OnKeyPressListener mOnkeyPressListener;    public void setOnKeyPressListener(OnKeyPressListener li) {
        mOnkeyPressListener = li;
    }    @Override
    public void onKey(int i, int[] ints) {        if (i == Keyboard.KEYCODE_DELETE && mOnkeyPressListener != null) {            //删除数据回调
            mOnkeyPressListener.onDeleteKey();

        } else if (i == Keyboard.KEYCODE_CANCEL && mOnkeyPressListener != null) {            //清除数据
            mOnkeyPressListener.onClearKey();

        } else if (i != KEYCODE_EMPTY) {            //添加数据回调
            mOnkeyPressListener.onInertKey(Character.toString((char) i));
        }
    }    @Override
    public void onPress(int i) {

    }    @Override
    public void onRelease(int i) {

    }    @Override
    public void onText(CharSequence charSequence) {

    }    @Override
    public void swipeRight() {        super.swipeRight();
    }    @Override
    public void swipeDown() {        super.swipeDown();
    }    @Override
    public void swipeLeft() {        super.swipeLeft();
    }    @Override
    public void swipeUp() {        super.swipeUp();
    }    /*****************************************************************/

    private int sp2px(float spValue) {        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;        return (int) (spValue * fontScale + 0.5f);
    }


    }

3、Activity 中实现

    public class MainActivity extends AppCompatActivity {    private EditText mEditText;    private NumKeyView mKeyView;    private LinearLayout mLinearlayout;    private PopupWindow mPop;    private View mPopView;    private RelativeLayout mRelativeLayout;    private Handler mhandler = new Handler() {        @Override
        public void handleMessage(Message msg) {            super.handleMessage(msg);
            mPop.dismiss();
            mKeyView.setVisibility(View.VISIBLE);
            ;
        }
    };    @Override
    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        mEditText = findViewById(R.id.et);
        mLinearlayout = findViewById(R.id.ln);


        mPop = new PopupWindow();
        mPopView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.keyboard_pop, null);
        mPop.setContentView(mPopView);
        mPop.setTouchable(true);
        mPop.setFocusable(true);
        mPop.setBackgroundDrawable(new ColorDrawable());
        mPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPop.setAnimationStyle(R.style.PopWindowstyle);
        mKeyView = mPopView.findViewById(R.id.keyboardview);
        mRelativeLayout = mPopView.findViewById(R.id.iv_hide);
        mRelativeLayout.setOnClickListener(new View.OnClickListener() {            @Override
            public void onClick(View v) {
                mPop.dismiss();
            }
        });


        mEditText.setOnTouchListener(new View.OnTouchListener() {            public boolean onTouch(View v, MotionEvent event) {                int inputType = mEditText.getInputType();
                mEditText.setInputType(InputType.TYPE_NULL);// 让系统键盘不弹出
                //点击按钮显示键盘
                mPop.showAtLocation(mLinearlayout, Gravity.BOTTOM, 0, 0);

                mEditText.setInputType(inputType);               //设定光标位置
                Selection.setSelection(mEditText.getText(), mEditText.getText().length());                return false;
            }
        });        

        //设置回调,并进行文本的插入与删除
        mKeyView.setOnKeyPressListener(new NumKeyView.OnKeyPressListener() {            @Override
            public void onInertKey(String text) {
                mEditText.append(text);
            }            @Override
            public void onDeleteKey() {                int last = mEditText.getText().length();                if (last > 0) {                    //删除最后一位
                    mEditText.getText().delete(last - 1, last);
                }
            }            @Override
            public void onClearKey() {
                mEditText.getText().clear();
            }
        });
    }


    }

5、keyboard_pop.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/c00000000"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/iv_hide"
            android:layout_width="80dp"
            android:layout_height="44dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="8dp"
            android:background="@color/cE7EDF5">

            <ImageView
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_calculator_up" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/iv_hide"
            android:background="@color/cCDCDCD">

            <com.example.guojin.keyboarddemo.NumKeyView
                android:id="@+id/keyboardview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="17dp"
                android:layout_marginTop="18dp"


                android:layout_marginRight="17dp"
                android:layout_marginBottom="20dp"
                android:focusable="true"
                android:focusableInTouchMode="true"


                app:bottomPadding="20dp"
                app:deleteDrawable="@drawable/ic_calculator_delete"
                app:keyBackgBackground="@drawable/key_bg_view"
                app:keyboardBackgBackground="@color/cCDCDCD" />
        </LinearLayout>
    
    </RelativeLayout>

style文件

    //键盘属性    <declare-styleable name="NumKeyView">
        <attr name="keyboardBackgBackground" format="color|reference"></attr>
        <attr name="keyBackgBackground" format="color|reference"></attr>
        <attr name="deleteDrawable" format="reference"></attr>
        <attr name="leftPadding" format="dimension"></attr>
        <attr name="rightPadding" format="dimension"></attr>
        <attr name="topPadding" format="dimension"></attr>
        <attr name="bottomPadding" format="dimension"></attr>
    </declare-styleable>



作者:涛涛123759
链接:https://www.jianshu.com/p/b899b8eb3055


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP