首页 > 其他分享 >6.17 5

6.17 5

时间:2024-06-17 23:56:07浏览次数:16  
标签:int 6.17 void keyboardView editText Override public

自定义软键盘

package com.zhen.accountbook.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.zhen.accountbook.R;

public class KeyBoardUtils {
    private final Keyboard k1;
    private KeyboardView keyboardView;
    private EditText editText;

    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 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() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.INVISIBLE || visibility == View.VISIBLE) {
            keyboardView.setVisibility(View.GONE);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="50dp"
android:keyWidth="25%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" 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>

 

标签:int,6.17,void,keyboardView,editText,Override,public
From: https://www.cnblogs.com/zzqq1314/p/18253470

相关文章

  • 6.17 7
    packagecom.zhen.accountbook.frag_record;importandroid.inputmethodservice.KeyboardView;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.widget.*;importandroidx.annotation.Nullable;importandroidx.fragment.app.Fragment;import......
  • 6.17 6
    packagecom.zhen.accountbook.utils;importandroid.app.Dialog;importandroid.content.Context;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid......
  • 6.17 8
    packagecom.zhen.accountbook.adapter;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ImageView;importandroid.widget.T......
  • 6.17 10
    <?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layou......
  • 6.17 9
    <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="wrap_con......
  • 2024.6.17鲜花/错误的号码
    XY星的星际新闻报一直不太畅销,所以报纸上会有一些广告,毕竟星际新闻局的非机器人员工也得吃饭。有一则广告是这样的:【数据删除】研学基地位于【数据删除】,该研学基地致力于让学生体验一个幻想纪前的生活并培养学生不借助现代高科技的群居生活能力。该研学基地将于幻想历元年六......
  • 6.17 学习心得
    这本书讲述了几十年前软件专案管理问题与经验,作者将大型系统开发比作一个焦油坑,我原本以为软件开发还是比较容易的,有了新想法,就会有新的软件产品出现,但是却不知道项目不能满足目标、进度、预算的要求,就不能成为一个好项目。程序,通过不同的途径转变成不同的产物,使之变得更有用,成本......
  • dart最新2024.06.17
    import'package:flutter/material.dart';voidmain(){runApp(constMyApp());}classMyAppextendsStatelessWidget{constMyApp({super.key});@overrideWidgetbuild(BuildContextcontext){returnconstMaterialApp(title:&......
  • 云原生周刊:Harbor v2.11 版本发布 | 2024.6.17
    开源项目推荐DeschedulerDescheduler是一个工具,可用于优化Kubernetes集群中Pod的部署位置。它可以找到可以移动的Pod,并将其驱逐,让默认调度器将它们重新调度到更合适的节点上。ProwlerProwler是一款适用于AWS、Azure、GCP和Kubernetes的开源安全工具,用于进行安全评......
  • Java速成笔记 2024.6.17版
    变量:可以变化的容器不同变量可以存储不同类型的值变量声明方法:变量类型变量名=初始值;E.G.inta=1;变量类型:整型:intlong浮点数:floatdouble布尔:boolean字符串:String字符:char变量命名注意事项:不能重名不能以数字开头常量:关键字:final语法:finalfl......