Android自定义实现漂亮的软键盘

工作中遇到了自定义软键盘的场景,虽然简单很快就实现了,但对个别的细节不太满意。

因为细节决定成败,所以细节之处也不能忽视。

先来张效果图吧:

  • key的相关属性:
  • row的相关属性:
  • KeyboardView的相关属性:
  • ASCII码对应表:

我对这个自定义软键盘做了个简单的封装,使用起来也很简单。以下是我的自定义软键盘View类:

package com.newcapec.visitorsystem.diyview;

import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.newcapec.cardliarbry.VistorCardController;
import com.newcapec.visitorsystem.R;
import com.newcapec.visitorsystem.interf.OnFinishListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author : xieqinzhong
 * @date :2020/6/16 14:52
 * @description:
 **/
public class AbcNumberView extends View {
    private Activity mActivity;
    private MyKeyboardView mKeyboardView;
    private EditText mEdit;

    /**
     * 数字与大写字母键盘
     */
    private Keyboard numberKeyboard;

    /*
    * 确认回调
    */
    private OnFinishListener finishListener;

    private KeyboardView keyboardView;

    /*
     *  id: 布局id
     *
     */
    public AbcNumberView(int viewId,Activity activity,boolean includeNumber, EditText edit,OnFinishListener finishListener) {
        super(activity);
        mActivity = activity;
        mEdit = edit;
        this.finishListener = finishListener;//R.xml.abc_and_number
        if (includeNumber) {
            numberKeyboard = new Keyboard(activity, R.xml.abc_and_number);
        }else {
            numberKeyboard = new Keyboard(activity, R.xml.abc_key);
        }
        mKeyboardView = (MyKeyboardView) activity.findViewById(viewId);
        mKeyboardView.setKeyboard(numberKeyboard);
        mKeyboardView.setEnabled(true);
        mKeyboardView.setPreviewEnabled(false);
        mKeyboardView.setOnKeyboardActionListener(listener);
        mKeyboardView.bringToFront();
    }

    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 = mEdit.getText();
            int start = mEdit.getSelectionStart();
            //判定是否是中文的正则表达式 [\\u4e00-\\u9fa5]判断一个中文 [\\u4e00-\\u9fa5]+多个中文
            if (primaryCode == -1) {// 确定键
                hideKeyboard();
                beginSearch(finishListener,mEdit.getText().toString());
            } else if (primaryCode == -3) {//删除键
                if (editable != null && editable.length() > 0) {
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }
            }else {
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }
    };

    private void beginSearch(final OnFinishListener onFinishListener, String value) {
        finishListener.search(value);

    }

    /**
     * 软键盘展示状态
     */
    public boolean isShow() {
        return mKeyboardView.getVisibility() == View.VISIBLE;
    }

    /**
     * 软键盘展示
     */
    public void showKeyboard() {
        int visibility = mKeyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            mKeyboardView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 软键盘隐藏
     */
    public void  hideKeyboard() {
        int visibility = mKeyboardView.getVisibility();
        if (visibility == View.VISIBLE) {
            mKeyboardView.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 禁掉系统软键盘
     */
    public void hideSoftInputMethod() {
        mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        String methodName = null;
        if (currentVersion >= 16) {
            // 4.2
            methodName = "setShowSoftInputOnFocus";
        } else if (currentVersion >= 14) {
            // 4.0
            methodName = "setSoftInputShownOnFocus";
        }
        if (methodName == null) {
            mEdit.setInputType(InputType.TYPE_NULL);
        } else {
            Class<EditText> cls = EditText.class;
            Method setShowSoftInputOnFocus;
            try {
                setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(mEdit, false);
            } catch (NoSuchMethodException e) {
                mEdit.setInputType(InputType.TYPE_NULL);
                e.printStackTrace();
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }


}

 布局文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="10px"
    android:keyWidth="160dp"
    android:keyHeight="90dp"
    android:verticalGap="1%p">
    <!--第一行-->
    <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="52"
            android:keyLabel="4" />
        <Key
            android:codes="53"
            android:keyLabel="5" />
        <Key
            android:codes="54"
            android:keyLabel="6" />
        <Key
            android:codes="55"
            android:keyLabel="7" />
        <Key
            android:codes="56"
            android:keyLabel="8" />
        <Key
            android:codes="57"
            android:keyLabel="9" />
        <Key
            android:codes="48"
            android:keyEdgeFlags="right"

            android:keyLabel="0" />
    </Row>

    <!--第二行-->
    <Row>
        <Key
            android:codes="81"
            android:keyLabel="Q" />
        <Key
            android:codes="87"
            android:keyLabel="W" />
        <Key
            android:codes="69"
            android:keyLabel="E" />
        <Key
            android:codes="82"
            android:keyLabel="R" />
        <Key
            android:codes="84"
            android:keyLabel="T" />
        <Key
            android:codes="89"
            android:keyLabel="Y" />
        <Key
            android:codes="85"
            android:keyLabel="U" />
        <Key
            android:codes="73"
            android:keyLabel="I" />
        <Key
            android:codes="79"
            android:keyLabel="O" />
        <Key
            android:codes="80"
            android:keyLabel="P" />
    </Row>

    <!--第三行-->
    <Row>
        <Key
            android:codes="65"
            android:keyLabel="A" />
        <Key
            android:codes="83"
            android:keyLabel="S" />
        <Key
            android:codes="68"
            android:keyLabel="D" />
        <Key
            android:codes="70"

            android:keyLabel="F" />
        <Key
            android:codes="71"


            android:keyLabel="G" />
        <Key
            android:codes="72"


            android:keyLabel="H" />
        <Key
            android:codes="74"


            android:keyLabel="J" />
        <Key
            android:codes="75"


            android:keyLabel="K" />
        <Key
            android:codes="76"
            android:keyLabel="L" />
        <Key
            android:codes="-1"
            android:keyIcon="@drawable/btn_ok"
            android:keyHeight="190dp" />
    </Row>

    <!--第四行-->
    <Row>
        <Key
            android:codes="90"
            android:keyLabel="Z" />
        <Key
            android:codes="88"


            android:keyLabel="X" />
        <Key
            android:codes="67"


            android:keyLabel="C" />
        <Key
            android:codes="86"


            android:keyLabel="V" />
        <Key
            android:codes="66"


            android:keyLabel="B" />
        <Key
            android:codes="78"


            android:keyLabel="N" />
        <Key
            android:codes="77"
            android:keyLabel="M" />

        <Key
            android:codes="-3"
            android:isRepeatable="false"

            android:keyIcon="@drawable/btn_del"
            android:keyWidth="330dp"/>
    </Row>

</Keyboard>

使用也很简单:

 mainBinding.includeBackscreenSearchLayout.edtInputPhone.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    abcNumberViewb.setOkListener(edtInputPhoneOkListener);
                    abcNumberViewb.setmEdit( mainBinding.includeBackscreenSearchLayout.edtInputPhone);
                    if(!abcNumberViewb.isShow()){
                        abcNumberViewb.showKeyboard();
                    }
                }
                return false;
            }
        });

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
Android 中,自定义软键盘需要实现一个继承自 InputMethodService 的服务,这个服务会在用户打开软键盘时被调用。下面是一些步骤来创建自定义软键盘: 1. 创建一个新的 Android 项目,并在 AndroidManifest.xml 文件中声明一个新的服务: ```xml <service android:name=".CustomKeyboard" android:label="Custom Keyboard" android:permission="android.permission.BIND_INPUT_METHOD"> <meta-data android:name="android.view.im" android:resource="@xml/method" /> </service> ``` 上面的代码声明了一个名为 CustomKeyboard 的服务,并将其与 android.view.im 绑定。在 res/xml 目录下创建一个名为 method.xml 的文件,用于指定 CustomKeyboard 的布局和行为: ```xml <?xml version="1.0" encoding="utf-8"?> <input-method xmlns:android="http://schemas.android.com/apk/res/android" android:settingsActivity=".SettingsActivity" android:imeSubtypeLocale="en_US" android:imeSubtypeMode="keyboard" > </input-method> ``` 上面的代码指定了键盘的设置活动、语言环境和子类型模式。 2. 创建 CustomKeyboard 类,并继承 InputMethodService。在这个类中,你需要重写一些回调方法,例如 onCreateInputView()、onKeyDown() 和 onStartInputView() 等。这些方法将决定键盘的外观和行为。 ```java public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView keyboardView; private Keyboard keyboard; @Override public View onCreateInputView() { keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null); keyboard = new Keyboard(this, R.xml.qwerty); keyboardView.setKeyboard(keyboard); keyboardView.setOnKeyboardActionListener(this); return keyboardView; } @Override public void onStartInputView(EditorInfo info, boolean restarting) { super.onStartInputView(info, restarting); keyboardView.setPreviewEnabled(false); } @Override public void onKey(int primaryCode, int[] keyCodes) { InputConnection ic = getCurrentInputConnection(); switch (primaryCode) { case Keyboard.KEYCODE_DELETE: ic.deleteSurroundingText(1, 0); break; case Keyboard.KEYCODE_SHIFT: // do something break; default: char c = (char) primaryCode; ic.commitText(String.valueOf(c), 1); } } } ``` 上面的代码创建了一个名为 CustomKeyboard 的类,并在 onCreateInputView() 方法中设置了键盘的布局和行为。在 onStartInputView() 方法中,我们禁用了键盘预览功能。在 onKey() 方法中,我们检查按下的键码并执行相应的操作。 3. 创建键盘布局。在 res/xml 目录下创建一个名为 qwerty.xml 的文件,用于指定键盘布局: ```xml <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="10%p" android:keyHeight="60dp" android:horizontalGap="0px" android:verticalGap="0px" android:keyEdgeFlags="left"> <Row> <Key android:keyLabel="q" android:keyEdgeFlags="left"/> <Key android:keyLabel="w"/> <Key android:keyLabel="e"/> <Key android:keyLabel="r"/> <Key android:keyLabel="t"/> <Key android:keyLabel="y"/> <Key android:keyLabel="u"/> <Key android:keyLabel="i"/> <Key android:keyLabel="o"/> <Key android:keyLabel="p" android:keyEdgeFlags="right"/> </Row> <Row> <Key android:keyLabel="a" android:keyEdgeFlags="left"/> <Key android:keyLabel="s"/> <Key android:keyLabel="d"/> <Key android:keyLabel="f"/> <Key android:keyLabel="g"/> <Key android:keyLabel="h"/> <Key android:keyLabel="j"/> <Key android:keyLabel="k"/> <Key android:keyLabel="l android:keyEdgeFlags="right"/> </Row> <Row> <Key android:keyLabel="shift" android:horizontalGap="10%p" android:keyWidth="20%p" android:keyEdgeFlags="left" android:isModifier="true" android:isSticky="true"/> <Key android:keyLabel="z"/> <Key android:keyLabel=""/> <Key android:keyLabel="c"/> <Key android:keyLabel="v"/> <Key android:keyLabel="b"/> <Key android:keyLabel="n"/> <Key android:keyLabel="m"/> <Key android:keyLabel="delete" android:keyWidth="20%p" android:keyEdgeFlags="right" android:icon="@drawable/ic_delete"/> </Row> <Row> <Key android:keyLabel="123" android:keyEdgeFlags="left" android:keyWidth="20%p"/> <Key android:keyLabel=" " android:keyWidth="40%p"/> <Key android:keyLabel="return" android:keyWidth="20%p" android:keyEdgeFlags="right"/> </Row> </Keyboard> ``` 上面的代码指定了一个基本的 QWERTY 键盘布局,包含字母、数字和删除键。 4. 运行应用程序并测试自定义软键盘。在测试键盘时,你需要在 Android 设备的输入法设置中激活你的自定义键盘。 以上就是创建自定义软键盘的基本步骤,你可以根据需要修改键盘的布局和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

特立独行的猫a

您的鼓励是我的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值