安卓实战:自定义软键盘 (1)

安卓实战:自定义软键盘

(注:安卓实战项目记账本的一部分内容)

在记账本中需要输入数字,萌生了想自己制作一个数字软键盘的内容,失败过程不眷数,直接展示最终成果
首先是页面布局:

key.xml

<?xml version="1.0" encoding="utf-8"?>
<!--keyHeight 每一个按键的高度   keyWidth:每一个按键宽度25% -->
<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"/>		//数字键1
        <Key android:codes="50" android:keyLabel="2"/>		//数字键2
        <Key android:codes="51" android:keyLabel="3"/>		//数字键3
        <Key android:codes="-5" android:keyLabel="删除"/>	//删除键
    </Row>
    <Row>
        <Key android:codes="52" android:keyLabel="4"/>		//数字键4
        <Key android:codes="53" android:keyLabel="5"/>		//数字键5
        <Key android:codes="54" android:keyLabel="6"/>		//数字键6
        <Key android:codes="-4" android:keyHeight="150dp" android:keyLabel="确定"/> 	//确定键,设置较大
    </Row>
    <Row>
        <Key android:codes="55" android:keyLabel="7"/>		//数字键7
        <Key android:codes="56" android:keyLabel="8"/>		//数字键8
        <Key android:codes="57" android:keyLabel="9"/>		//数字键9
    </Row>
    <Row>
        <Key android:codes="-3" android:keyLabel="清零"/>	//清零键
        <Key android:codes="48" android:keyLabel="0"/>		//0键
        <Key android:codes="46" android:keyLabel="."/>		//.键
    </Row>
</Keyboard>


然后是逻辑编写:

KeyboardUtils.java

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

public class KeyBoardUtils {
  private final Keyboard k1;    //自定义键盘
  private KeyboardView keyboardView;
  private EditText 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 primaryCode) {
      }
      @Override
      public void onRelease(int primaryCode) {
      }
      @Override
      public void onKey(int primaryCode, int[] keyCodes) {
          Editable editable = editText.getText();
          int start = editText.getSelectionStart();
          switch (primaryCode) {
              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)primaryCode));
                  break;
          }
      }
      @Override
      public void onText(CharSequence text) {
      }
      @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.VISIBLE||visibility==View.INVISIBLE) {
          keyboardView.setVisibility(View.GONE);
      }
  }
}

成果图:
![(https://img-blog.csdnimg.cn/20210104110912554.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTE2NjE5NQ==,size_16,color_FFFFFF,t_70#pic_center)

有关于这个的问题可以随时找我交流

原文链接

作者:黄书竞

猜你喜欢

转载自blog.csdn.net/fjnu_se/article/details/112230382