效果图:
因为我们是在售货机界面上;所以控件的单位不要参照;
1.在需要的调用软键盘activity_main.xml中加入键盘控件
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<android.inputmethodservice.KeyboardView
android:id="@+id/dialog_login_keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorGray"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingRight="32dp"
android:shadowRadius="0.0"
android:keyBackground="@drawable/bg_keyboard_selector"
android:keyPreviewLayout="@null"
android:keyTextColor="@color/keyTextColor"
android:keyTextSize="60dp"
android:labelTextSize="48dp"
android:visibility="gone" />
</RelativeLayout>
1.2:背景选择器:bg_keyboard_selector;这里用的是自定义圆角的两个不同颜色的shape 文件;大家根据需求可以修改;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/rounded_10_light_grey_fill"/>
<item android:drawable="@drawable/rounded_10_white_fill"/>
</selector>
2.然后在res文件夹下新建文件夹,里面新建三个文件,即每种键盘样式的布局分别为keyboard_numbers.xml, keyboard_punctuate.xml, keyboard_qwerty.xml
2.1:keyboard_numbers.xml:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="180dp"
android:horizontalGap="30px"
android:verticalGap="20px"
android:keyHeight="90dp">
<Row >
<Key android:codes="57419" android:keyLabel="←" />
<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:keyIcon="@drawable/sym_keyboard_delete"
android:isRepeatable="true" />
</Row>
<Row>
<Key android:codes="57421" android:keyLabel="→" />
<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:isRepeatable="true" android:keyLabel="完成" />
</Row>
<Row >
<Key android:codes="-7" android:isRepeatable="true"
android:keyLabel="\#+=" />
<Key android:codes="55" android:keyLabel="7" />
<Key android:codes="56" android:keyLabel="8" />
<Key android:codes="57" android:keyLabel="9" />
<Key android:codes="32" android:keyIcon="@drawable/sym_keyboard_space" />
</Row>
<Row >
<Key android:codes="-2" android:keyLabel="ABC" />
<Key android:codes="44" android:keyLabel="," />
<Key android:codes="48" android:keyLabel="0" />
<Key android:codes="46" android:keyLabel="." />
<Key android:codes="-3" android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_cancle" />
</Row>
</Keyboard>
2.2:keyboard_punctuate.xml:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="17dp"
android:keyHeight="90dp"
android:keyWidth="90dp"
android:verticalGap="20px">
<Row>
<Key
android:codes="126"
android:keyLabel="~" />
<Key
android:codes="33"
android:keyLabel="!" />
<Key
android:codes="64"
android:keyLabel="\@" />
<Key
android:codes="35"
android:keyLabel="#" />
<Key
android:codes="36"
android:keyLabel="$" />
<Key
android:codes="37"
android:keyLabel="%" />
<Key
android:codes="94"
android:keyLabel="^" />
<Key
android:codes="38"
android:keyLabel="&" />
<Key
android:codes="42"
android:keyLabel="*" />
<Key
android:codes="40"
android:keyLabel="(" />
</Row>
<Row>
<Key
android:horizontalGap="63dp"
android:codes="96"
android:keyLabel="`" />
<Key
android:codes="45"
android:keyLabel="-" />
<Key
android:codes="43"
android:keyLabel="+" />
<Key
android:codes="61"
android:keyLabel="=" />
<Key
android:codes="91"
android:keyLabel="[" />
<Key
android:codes="93"
android:keyLabel="]" />
<Key
android:codes="123"
android:keyLabel="{" />
<Key
android:codes="125"
android:keyLabel="}" />
<Key
android:codes="41"
android:keyLabel=")" />
</Row>
<Row>
<Key
android:codes="-7"
android:isModifier="true"
android:isSticky="true"
android:keyEdgeFlags="left"
android:keyLabel="\?123"
android:keyWidth="142dp" />
<Key
android:codes="95"
android:keyLabel="_" />
<Key
android:codes="58"
android:keyLabel=":" />
<Key
android:codes="59"
android:keyLabel=";" />
<Key
android:codes="39"
android:keyLabel="'" />
<Key
android:codes="34"
android:keyLabel=""" />
<Key
android:codes="92"
android:keyLabel="\\" />
<Key
android:codes="124"
android:keyLabel="|" />
<Key
android:codes="-5"
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyIcon="@drawable/sym_keyboard_delete"
android:keyWidth="142dp" />
</Row>
<Row android:rowEdgeFlags="bottom">
<Key
android:codes="-2"
android:keyLabel="ABC"
android:keyWidth="142dp" />
<Key
android:codes="60"
android:keyLabel="<" />
<Key
android:codes="62"
android:keyLabel=">" />
<Key
android:codes="44"
android:keyLabel="," />
<Key
android:codes="32"
android:isRepeatable="true"
android:rowEdgeFlags="bottom"
android:keyIcon="@drawable/sym_keyboard_space" />
<Key
android:codes="46"
android:keyLabel="." />
<Key
android:codes="63"
android:keyLabel="\?" />
<Key
android:codes="47"
android:keyLabel="/" />
<Key
android:codes="-4"
android:keyEdgeFlags="right"
android:keyLabel="完成"
android:keyWidth="142dp" />
</Row>
</Keyboard>
2.3:keyboard_qwerty.xml:
<?xml version="1.0" encoding="utf-8"?>
<Keyboard
xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="90dp"
android:keyWidth="90dp"
android:horizontalGap="17dp"
android:verticalGap="20px"
>
<Row>
<Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left" />
<Key android:codes="119" android:keyLabel="w" />
<Key android:codes="101" android:keyLabel="e" />
<Key android:codes="114" android:keyLabel="r" />
<Key android:codes="116" android:keyLabel="t" />
<Key android:codes="121" android:keyLabel="y" />
<Key android:codes="117" android:keyLabel="u" />
<Key android:codes="105" android:keyLabel="i" />
<Key android:codes="111" android:keyLabel="o" />
<Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right" />
</Row>
<Row>
<Key android:horizontalGap="63dp" android:codes="97"
android:keyEdgeFlags="left" android:keyLabel="a" />
<Key android:codes="115" android:keyLabel="s" />
<Key android:codes="100" android:keyLabel="d" />
<Key android:codes="102" android:keyLabel="f" />
<Key android:codes="103" android:keyLabel="g" />
<Key android:codes="104" android:keyLabel="h" />
<Key android:codes="106" android:keyLabel="j" />
<Key android:codes="107" android:keyLabel="k" />
<Key android:codes="108" android:keyEdgeFlags="right"
android:keyLabel="l" />
</Row>
<Row>
<Key android:keyWidth="142dp" android:codes="-1"
android:keyEdgeFlags="left" android:isModifier="true"
android:isSticky="true" android:keyIcon="@drawable/sym_keyboard_shift" />
<Key android:codes="122" android:keyLabel="z" />
<Key android:codes="120" android:keyLabel="x" />
<Key android:codes="99" android:keyLabel="c" />
<Key android:codes="118" android:keyLabel="v" />
<Key android:codes="98" android:keyLabel="b" />
<Key android:codes="110" android:keyLabel="n" />
<Key android:codes="109" android:keyLabel="m" />
<Key android:keyWidth="142dp" android:codes="-5"
android:keyEdgeFlags="right" android:isRepeatable="true"
android:keyIcon="@drawable/sym_keyboard_delete" />
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:keyWidth="142dp" android:codes="-2"
android:keyLabel="\?123" />
<Key android:codes="64" android:keyLabel="\@" />
<Key android:keyWidth="414dp" android:codes="32"
android:isRepeatable="true" android:keyIcon="@drawable/sym_keyboard_space" />
<Key android:codes="44"
android:keyLabel="," />
<Key android:codes="46"
android:keyLabel="." />
<Key android:keyWidth="142dp" android:codes="-4"
android:keyEdgeFlags="right" android:keyLabel="完成" />
</Row>
</Keyboard>
2.3:按键的选择器:
3.KeyBoardUtil工具类
这里我们有个界面需求是只显示数字键盘;所以根据 EditText 的输入类型做了判断;
android:inputType="numberDecimal" 不能切换键盘;
package com.zhishoubao.vending.utils;
import android.content.Context;
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 android.widget.TextView;
import com.zhishoubao.vending.R;
import java.util.List;
/**
* Created by zhuanghongji on 2015/12/10.
*/
public class KeyboardUtil {
private Context mContext;
private KeyboardView mKeyboardView;
private Keyboard mNumberKeyboard; // 数字键盘
private Keyboard mLetterKeyboard; // 字母键盘
private Keyboard mPunctuateKeyboard; // 标点键盘
private boolean isNumber = false; // 是否数字键盘
private boolean isUpper = false; // 是否大写
private boolean isPun = false; // 是否标点
private EditText mEditText;
public final int KEYCODE_PUN = -7;
public final int NUMBERDECIMAL = 0x00002002;//只显示数字键盘
public int inputType ; // 0:字母键盘,1:数字键盘,2:字符键盘
private OnClickDone onClickDone;
public void setOnClickDone(OnClickDone onClickDone) {
this.onClickDone = onClickDone;
}
public interface OnClickDone{
void onDone();
}
public KeyboardUtil(Context context, KeyboardView view, int inputType) {
this.inputType = inputType;
this.mContext = context;
this.isNumber = inputType == 1;
mNumberKeyboard = new Keyboard(mContext, R.xml.keyboard_numbers);
mLetterKeyboard = new Keyboard(mContext, R.xml.keyboard_qwerty);
mPunctuateKeyboard = new Keyboard(mContext, R.xml.keyboard_punctuate);
mKeyboardView = view;
List<Keyboard.Key> mNumberKeyList = mNumberKeyboard.getKeys();
for (Keyboard.Key key : mNumberKeyList) {
if(key!=null ){
if( key.codes[0] == -2 || key.codes[0] == -3|| key.codes[0] == -4
||key.codes[0] == -5 || key.codes[0] == -7||key.codes[0] == 32
||key.codes[0] == 46||key.codes[0] == 57419||key.codes[0] == 57421
||key.codes[0] == 44) {
key.onPressed();
}
}
}
List<Keyboard.Key> mLetterKeyList = mLetterKeyboard.getKeys();
for (Keyboard.Key key : mLetterKeyList) {
if(key!=null ){
if(key.codes[0] == -1 || key.codes[0] == -2 || key.codes[0] == -4||key.codes[0] == -5){
key.onPressed();
}
}
}
List<Keyboard.Key> mPunctuateKeyList = mPunctuateKeyboard.getKeys();
for (Keyboard.Key key : mPunctuateKeyList) {
if(key!=null ){
if( key.codes[0] == -2 || key.codes[0] == -4||key.codes[0] == -5 || key.codes[0] == -7) {
key.onPressed();
}
}
}
switch (inputType){
case 0:
mKeyboardView.setKeyboard(mLetterKeyboard);
break;
case 1:
mKeyboardView.setKeyboard(mNumberKeyboard);
break;
case 2:
mKeyboardView.setKeyboard(mPunctuateKeyboard);
break;
default:
mKeyboardView.setKeyboard(mLetterKeyboard);
break;
}
mKeyboardView.setEnabled(true);
mKeyboardView.setPreviewEnabled(false);
mKeyboardView.setOnKeyboardActionListener(listener);
}
private 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 = mEditText.getText();
int start = mEditText.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) { // cancel
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DONE) { // done
if(onClickDone!=null){
onClickDone.onDone();
}
} else if (primaryCode == Keyboard.KEYCODE_DELETE) { // 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) { // 大小写切换
changeKeyboart();
mKeyboardView.setKeyboard(mLetterKeyboard);
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) { // 数字键盘切换
if (isNumber) {
if(mEditText.getInputType() == NUMBERDECIMAL){
return;
}
isNumber = false;
mKeyboardView.setKeyboard(mLetterKeyboard);
} else {
if(isPun){
isPun = false;
mKeyboardView.setKeyboard(mLetterKeyboard);
}else{
isNumber = true;
mKeyboardView.setKeyboard(mNumberKeyboard);
}
}
}else if(primaryCode== KEYCODE_PUN){
if(mEditText.getInputType() == NUMBERDECIMAL){
return;
}
if(isPun){
isPun = false;
isNumber =true;
mKeyboardView.setKeyboard(mNumberKeyboard);
}else {
isPun = true;
isNumber = false;
mKeyboardView.setKeyboard(mPunctuateKeyboard);
}
}else if (primaryCode == 57419) { // 左移
if (start > 0) {
mEditText.setSelection(start - 1);
}
} else if (primaryCode == 57421) { // 右移
if (start < mEditText.length()) {
mEditText.setSelection(start + 1);
}
} else { // 输入键盘值
editable.insert(start, Character.toString((char) primaryCode));
}
}
@Override
public void onText(CharSequence text) {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeDown() {
}
@Override
public void swipeUp() {
}
};
private void changeKeyboart() {
List<Keyboard.Key> keyList = mLetterKeyboard.getKeys();
if (isUpper) { // 大写切换小写
isUpper = false;
for (Keyboard.Key key : keyList) {
if(key.codes[0] == -1){
key.onPressed();
}
if (key.label != null && isLetter(key.label.toString())) {
key.label = key.label.toString().toLowerCase();
key.codes[0] = key.codes[0] + 32;
}
}
} else { // 小写切换成大写
isUpper = true;
for (Keyboard.Key key : keyList) {
if(key.codes[0] == -1){
key.onPressed();
}
if (key.label != null && isLetter(key.label.toString())) {
key.label = key.label.toString().toUpperCase();
key.codes[0] = key.codes[0] - 32;
}
}
}
}
/**
* 判断是否是字母
*/
private boolean isLetter(String str) {
String wordStr = "abcdefghijklmnopqrstuvwxyz";
return wordStr.contains(str.toLowerCase());
}
public void hideKeyboard() {
int visibility = mKeyboardView.getVisibility();
if (visibility == View.VISIBLE) {
mKeyboardView.setVisibility(View.INVISIBLE);
}
}
public void showKeyboard(EditText editText, OnClickDone onClickDone) {
mEditText = editText;
this.onClickDone = onClickDone;
int visibility = mKeyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
mKeyboardView.setVisibility(View.VISIBLE);
}
}
}
4:调用:
keyboardView = (KeyboardView) view.findViewById(R.id.dialog_change_price_keyboard_view); edtprice = (EditText) view.findViewById(R.id.dialog_change_price_edt_price); //0:英文字母键盘 ;1:代表显示数字键盘;2字符键盘; keyboardUtil = new KeyboardUtil(context, keyboardView,1); //这里对完成按键做了回调;逻辑可以自己写
edtprice.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { keyboardUtil.showKeyboard(edtprice, new KeyboardUtil.OnClickDone() { @Override public void onDone() { if (edtprice.getText().toString().isEmpty()) { UiUtils.toast(context, "价格不能为空"); return; } if (Double.valueOf(edtprice.getText().toString()) <= 0) { UiUtils.toast(context, "价格不能为0元"); } else { callbackInfo.onSave(Double.valueOf(edtprice.getText().toString())); } } }); return false; } });
点击软键盘外部,隐藏软键盘:
@Override public boolean dispatchTouchEvent(MotionEvent ev) { if (null != this.getCurrentFocus() && !inRangeOfView(keyboardView, ev)) { keyboardView.setVisibility(View.GONE); } return super.dispatchTouchEvent(ev); }
参考博客:https://blog.csdn.net/qq_37980878/article/details/79023794