自定义的键盘布局和颜色等形式,识别点击识别,正确将内容绑定在
编写自定义布局文件
jianpan.xml
属性解释:
keyHeight 软键盘一个键的高度
keyWidth=“25%p” 软键盘一个键的占比
horizontalGap 横向间隔
verticalGap 纵向间隔
codes里值得ASCII值
<?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="55" android:keyLabel="7"></Key> <Key android:codes="56" android:keyLabel="8"></Key> <Key android:codes="57" android:keyLabel="9"></Key> <Key android:codes="-1" android:keyLabel="删除"></Key> </Row> <Row> <Key android:codes="52" android:keyLabel="4"></Key> <Key android:codes="53" android:keyLabel="5"></Key> <Key android:codes="54" android:keyLabel="6"></Key> <Key android:codes="-2" android:keyLabel="确定" android:keyHeight="150dp" ></Key> </Row> <Row> <Key android:codes="49" android:keyLabel="1"></Key> <Key android:codes="50" android:keyLabel="2"></Key> <Key android:codes="51" android:keyLabel="3"></Key> </Row> <Row> <Key android:codes="-3" android:keyLabel="清零"></Key> <Key android:codes="48" android:keyLabel="0"></Key> <Key android:codes="46" android:keyLabel="."></Key> </Row> </Keyboard>
设置键盘的点击事件
独立的JAVA类
public class Jianpan { private KeyboardView keyboardView; private EditText editText; private Keyboard mykey;//自定义键盘 public interface OnEnsureListener{ public void onEnsure(); } OnEnsureListener onEnsureListener; public void setOnEnsureListener(OnEnsureListener onEnsureListener) { this.onEnsureListener = onEnsureListener; } public Jianpan(KeyboardView keyboardView, EditText editText) { this.keyboardView = keyboardView; this.editText = editText; this.editText.setInputType(InputType.TYPE_NULL);//不弹出系统键盘 mykey = new Keyboard(this.editText.getContext(), R.xml.jianpan); this.keyboardView.setKeyboard(mykey);//设置要显示键盘的样式 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(); if(i==-1){//删除 if(editable!=null&&editable.length()>0){ if(start>0){ editable.delete(start-1,start); } } } else if(i==-2){//确定 onEnsureListener.onEnsure();//接口回调 System.out.println(editText.getText().toString()); } else if(i==-3){//清零 editable.clear(); } else{ editable.insert(start,Character.toString((char)i)); } } @Override public void onText(CharSequence charSequence) { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeDown() { } @Override public void swipeUp() { } }; public void show(){ //显示键盘 int now=keyboardView.getVisibility(); if(now==View.INVISIBLE||now==View.GONE){ keyboardView.setVisibility(View.VISIBLE); } } public void hide(){ //隐藏键盘 int now=keyboardView.getVisibility(); if(now==View.VISIBLE||now==View.INVISIBLE){ keyboardView.setVisibility(View.GONE); } } }
主页面的JAVA代码调用自定义软键盘
Jianpan jp=new Jianpan(my,et); jp.show(); jp.setOnEnsureListener(new Jianpan.OnEnsureListener() { @Override public void onEnsure() { String nowmoney=et.getText().toString(); if(TextUtils.isEmpty(nowmoney)||nowmoney.equals("0")) { getActivity().finish(); return; } getActivity().finish(); return ; } });
线没咋对齐,懒得调了。