软键盘的绘制以及逻辑编写

在res包下创建一个xml文件夹,创立一个key.xml

<?xml version="1.0" encoding="utf-8"?>

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyHeight="50dp"
    android:keyWidth="20%p"
    android:horizontalGap="1px"
    android:verticalGap="1px">
    <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:keyLabel="删除"/>
    </Row>
    <Row>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="-4" android:keyHeight="150dp" android:keyLabel="确定"/>
    </Row>

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

    <Row>
        <Key android:codes="-3" android:keyLabel="清零"/>
        <Key android:codes="48" android:keyLabel="0"/>
        <Key android:codes="46" android:keyLabel="."/>
    </Row>
</Keyboard>

建立对应的utils的包

KeyBoardUtils

package com.example.myapplication.utils;

import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

import com.example.myapplication.R;

public class KeyBoardUtils {
    
    private KeyboardView keyboardView;
    private EditText editText;
    private final Keyboard k1;//自定义键盘

    public interface OnEnsureListener{
        public void onEnsure();
    }
    
    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
        
    }

    public KeyBoardUtils(KeyboardView keyboardView, EditText editText) {
        this.keyboardView = keyboardView;
        this.editText = editText;
        this.editText.setInputType(InputType.TYPE_NULL);//取消弹出系统键盘
        k1 = new Keyboard(this.editText.getContext(), R.xml.key);
        
        
        this.keyboardView.setKeyboard(k1);//设置显示的键盘的样式
        this.keyboardView.setEnabled(true);
        this.keyboardView.setPreviewEnabled(false);
        this.keyboardView.setOnKeyboardActionListener(listener);//设置键盘按钮被点击的监听
    }
    
    KeyboardView.OnKeyboardActionListener listener=new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {
            
        }

        @Override
        public void onRelease(int primaryCode) {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = editText.getText();
            int start = editText.getSelectionStart();
            switch (primaryCode) {
                case Keyboard.KEYCODE_DELETE: //点击删除键
                    if (editable!=null&&editable.length()>0){
                        if(start>0){
                            editable.delete(start-1,start);
                        }
                    }
                    break;
                case Keyboard.KEYCODE_CANCEL: //点击了清零
                    editable.clear();
                    break;
                case Keyboard.KEYCODE_DONE: //点击了完成
                    onEnsureListener.onEnsure();//通过接口回调,点击确定,可以调用这个方法
                    break;
                default://其他数字直接插入
                    editable.insert(start,Character.toString((char)primaryCode));
                    break;
            }
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeRight() {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void swipeUp() {
        }
    };
    
    //显示键盘
    public void showKeyboard(){
        int visibility = keyboardView.getVisibility();
        if(visibility== View.INVISIBLE||visibility==View.GONE){
            keyboardView.setVisibility(View.VISIBLE);
        }
    }
    
    //隐藏键盘
    public void hideKeyboard(){
        int visibility = keyboardView.getVisibility();
        if(visibility==View.VISIBLE||visibility==View.INVISIBLE){
            keyboardView.setVisibility(View.GONE);
        }
    }
    
}

posted on 2024-02-19 22:41  许七安gyg  阅读(8)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });