【android-自定义键盘的设置】

效果图

【android-自定义键盘的设置】_第1张图片

第一步,在哪个地方放自定义的键盘?那个地方放输入?

我打算在这个最下面放我的键盘
【android-自定义键盘的设置】_第2张图片
因此在这个xml文件中添加键盘的组件,为它设置参数,给它id,长,宽,背景,字体颜色,是否聚焦等


    <android.inputmethodservice.KeyboardView
        android:id="@+id/frag_record_rl_key"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:keyBackground="@color/grey_f3f3f3"
        android:keyTextColor="@color/black"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:paddingTop="1dp"
        android:layout_alignParentBottom="true"
        android:shadowColor="@color/white"
        android:shadowRadius="0.0"/>
        

输入的地方 EditText为输入文本的地方

        <EditText
            android:id="@+id/frag_rl_top_money"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:text="0"
            android:background="@color/white"
            />

第二部,设置键盘

第一步我们确定了键盘的位置,现在详细设置这个键盘,我们在res目录下创造一个xml的文件夹,在此文件夹中新建一个key.xml的文件,这个文件将详细设置键盘,具体到每一个按键。
android:keyHeight,android:keyWidth为键盘按键的高度,宽度
code表示代码这个是有规定的,lable为真正代表的内容


<Keyboard xmlns:android="http://schemas.android.com/apk/res/android">
    <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" 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>
Keyboard>

第三步,键盘的逻辑代码编写

在java文件夹下建立utils文件夹 keyutils

package com.example.notebook.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.notebook.R;

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

    public keyutils(Keyboard k1, KeyboardView keyboardView, EditText editText) {
        this.k1 = k1;
        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);//设置键盘按钮被点击了的监听
    }

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

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

    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 showKey(){
        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);
        }
    }
}

你可能感兴趣的:(android,android,android,studio,java)