美文网首页
手动写一个简易全键盘

手动写一个简易全键盘

作者: 大鸭梨leepear | 来源:发表于2017-04-09 22:09 被阅读148次

最近需要做一个全键盘的需求,所以记录和分享一下思路。
上来不多说直接上代码:

1.这是一个工具类,可以直接放到代码里使用:

package com.example.administrator.keyboard;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;

import java.util.List;

/**
 * Created by Lee on 2017/4/9.
 */
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(false);
        keyboardView.setOnKeyboardActionListener(listener);
    }

    private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.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<Keyboard.Key> keylist = k1.getKeys();
        if (isupper) {//大写切换小写
            isupper = false;
            for(Keyboard.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(Keyboard.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;
    }

}

2.具体实现方式:

package com.example.administrator.keyboard;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by Administrator on 2017/4/9.
 */
public class NewFragment extends Fragment{
    private EditText editText1;
    private EditText edit1;
    private Context ctx;
    private Activity act;
    private Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ctx=getContext();
        act=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment,container,false);
        editText1= (EditText) view.findViewById(R.id.edit);
        edit1= (EditText) view.findViewById(R.id.edit1);
        button= (Button) view.findViewById(R.id.btn);
        editText1.setInputType(InputType.TYPE_NULL);
        editText1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                new KeyBoardUtil(act, ctx, editText1).showKeyboard();
                return false;
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        edit1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int inputback = edit1.getInputType();
                edit1.setInputType(InputType.TYPE_NULL);
                new KeyBoardUtil(act, ctx, edit1).showKeyboard();
                edit1.setInputType(inputback);

                return false;
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s=edit1.getText().toString();
                Log.i("look",s);
            }
        });

    }
}

3.接下来就是布局文件了:
xml文件1:

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="10.000002%p" android:keyHeight="@dimen/key_height"
    android:horizontalGap="0.0px" android:verticalGap="0.0px"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Row>
        <Key android:codes="113" android:keyEdgeFlags="left"
            android:keyLabel="q" />
        <Key android:codes="119" android:keyLabel="w" />
        <Key android:codes="101" android:keyLabel="e" />
        <Key android:codes="114" android:keyLabel="r" />
        <Key android:codes="116" android:keyLabel="t" />
        <Key android:codes="121" android:keyLabel="y" />
        <Key android:codes="117" android:keyLabel="u" />
        <Key android:codes="105" android:keyLabel="i" />
        <Key android:codes="111" android:keyLabel="o" />
        <Key android:codes="112" android:keyEdgeFlags="right"
            android:keyLabel="p" />
    </Row>
    <Row>
        <Key android:horizontalGap="4.999995%p" android:codes="97"
            android:keyEdgeFlags="left" android:keyLabel="a" />
        <Key android:codes="115" android:keyLabel="s" />
        <Key android:codes="100" android:keyLabel="d" />
        <Key android:codes="102" android:keyLabel="f" />
        <Key android:codes="103" android:keyLabel="g" />
        <Key android:codes="104" android:keyLabel="h" />
        <Key android:codes="106" android:keyLabel="j" />
        <Key android:codes="107" android:keyLabel="k" />
        <Key android:codes="108" android:keyEdgeFlags="right"
            android:keyLabel="l" />
    </Row>
    <Row>
        <Key android:keyWidth="14.999998%p" android:codes="-1"
            android:keyEdgeFlags="left" android:isModifier="true"
            android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />
        <Key android:codes="122" android:keyLabel="z" />
        <Key android:codes="120" android:keyLabel="x" />
        <Key android:codes="99" android:keyLabel="c" />
        <Key android:codes="118" android:keyLabel="v" />
        <Key android:codes="98" android:keyLabel="b" />
        <Key android:codes="110" android:keyLabel="n" />
        <Key android:codes="109" android:keyLabel="m" />
        <Key android:keyWidth="14.999998%p" android:codes="-5"
            android:keyEdgeFlags="right" android:isRepeatable="true"
            android:keyIcon="@drawable/sym_keyboard_delete" />
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:keyWidth="20.000004%p" android:codes="-2"
            android:keyLabel="12#" />
        <Key android:keyWidth="14.999998%p" android:codes="44"
            android:keyLabel="," />
        <Key android:keyWidth="29.999996%p" android:codes="32"
            android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
        <Key android:keyWidth="14.999998%p" android:codes="46"
            android:keyLabel="." />
        <Key android:keyWidth="20.000004%p" android:codes="-3"
            android:keyEdgeFlags="right" android:keyLabel="完成" />
    </Row>
</Keyboard>

xml文件2

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="25%p" android:horizontalGap="0px"
    android:verticalGap="0px" android:keyHeight="@dimen/key_height">
    <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="57419" android:keyEdgeFlags="right"
            android:keyIcon="@drawable/sym_keyboard_left" />
    </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="57421" android:keyEdgeFlags="right"
            android:keyIcon="@drawable/sym_keyboard_right" />
    </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="-3" android:keyHeight="100dip"
            android:keyEdgeFlags="right" android:isRepeatable="true"
            android:keyLabel="完成" />
    </Row>
    <Row>
        <Key android:codes="-2" android:keyLabel="ABC" />
        <Key android:codes="48" android:keyLabel="0" />
        <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" />
    </Row>
</Keyboard>

好了,到此结束。

webwxgetmsgimg.jpg

![Uploading webwxgetmsgimg (1)_094484.jpg . . .]
![Uploading webwxgetmsgimg_154590.jpg . . .]

相关文章

  • 手动写一个简易全键盘

    最近需要做一个全键盘的需求,所以记录和分享一下思路。上来不多说直接上代码: 1.这是一个工具类,可以直接放到代码里...

  • Numpy简易入门笔记

    来自 AI基础:Numpy简易入门手动运行了一遍实例代码,笔记待查于此处。 Numpy 简易入门 Numpy是 N...

  • QAP手动关闭键盘

    QAP安卓端在弹出键盘时,我们点击键盘上的完成按钮,键盘会自动收起。如果输入完成后未点击完成按钮,直接点击保存等相...

  • js初识第三节

    一个可以设置是否循环播放的简易手动轮播 节点关系parentNode和firstChild以及和firstElem...

  • Android 随笔-先记录以后可扩展

    设置EditText获取焦点并弹出软键盘及手动关闭软键盘 设置EditText获取焦点并弹出软键盘 关闭软键盘 让...

  • 键盘类型(keyboradType)

    iOS里面有自带的有12种键盘类型。主要分为两大类:全键盘和9宫格键盘 全键盘 1、Default 2、AS...

  • 对于防爆葫芦您了解多少

    防爆葫芦是一种使用简易、携带方便的手动起重机械,防爆手拉葫芦可与防爆手动单轨小车配套使用组成手动起重运输小车,防爆...

  • 安卓设置EditText获取焦点并弹出软键盘及手动关闭软键盘

    设置EditText获取焦点并弹出软键盘 手动关闭软键盘 安卓开发技术分享: https://www.jiansh...

  • iOS 手动解决键盘遮挡

    使用 IQKeyboardManager 这个开源库,基本可以应对大部分情况,但是在某些情况下,这个框架会有bug...

  • 三种样式的轮播图

    一、100%比例轮播图 HTML代码 CSS样式 js代码 二、手动箭头轮播图 三、简易轮播图

网友评论

      本文标题:手动写一个简易全键盘

      本文链接:https://www.haomeiwen.com/subject/fjjjattx.html