1. 程式人生 > 其它 >安卓實戰:自定義軟鍵盤 (1)

安卓實戰:自定義軟鍵盤 (1)

技術標籤:Androidandroidandroid studio

安卓實戰:自定義軟鍵盤

(注:安卓實戰專案記賬本的一部分內容)

在記賬本中需要輸入數字,萌生了想自己製作一個數字軟鍵盤的內容,失敗過程不眷數,直接展示最終成果
首先是頁面佈局:

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)

有關於這個的問題可以隨時找我交流

原文連結

作者:黃書競