每日总结2023/2/27

今天学习了Android Studio中自定义软键盘的简单编写

界面效果如下

 

 

 过程如下

在xml文件下新建.xml文件作为键盘布局

复制代码
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="1px"
    android:keyWidth="25%p"
    android:keyHeight="50dp"
    android:verticalGap="1px">
    <!--keyHeight   按键高度-->
    <!--keyWidth    按键宽度-->
    <!--horizontalGap   水平方向分割-->
    <!--verticalGap     垂直方向。。-->
    <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>
复制代码

 

在java文件中编写相关内容

  

复制代码
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.daka.R;

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

    public interface OnEnsureListener {          //接口
        public void onEnsure();
    }

    OnEnsureListener OnEnsureListener;

    public void setOnEnsureListener(KeyBoardUtils.OnEnsureListener onEnsureListener) {
        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 i) {

        }

        @Override
        public void onRelease(int i) {

        }

        @Override
        public void onKey(int i, int[] ints) {
            Editable editable = editText.getText();
            int start = editText.getSelectionStart();
            switch (i) {
                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) i));

                    break;

            }
        }

        @Override
        public void onText(CharSequence charSequence) {

        }

        @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(){
        visibility1 = keyboardView.getVisibility();
        if (visibility1== View.VISIBLE||visibility1==View.INVISIBLE) {
            keyboardView.setVisibility(View.GONE);

        }
    }*/
}
复制代码

 

在具体界面中写软键盘调用内容

复制代码
<!--自定义软键盘-->
    <!--focusable   获取焦点-->
    <!--fadeScrollbars  触摸时获取焦点-->
    <android.inputmethodservice.KeyboardView
        android:id="@+id/record_keyboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/gray"
        android:fadeScrollbars="true"
        android:focusable="true"
        android:keyTextColor="@color/black"
        android:paddingTop="1dp"
        android:shadowColor="@color/white"
        android:shadowRadius="0.0" />
复制代码

 

posted @   花伤错零  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示