1.在你要写键盘的XML文件中给自定义键盘占个地
<!--自定义键盘--> <androID.inputmethodservice.KeyboardVIEw androID:ID="@+ID/frag_record_keyboard" 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:layout_alignParentBottom="true" androID:shadowcolor="@color/white" androID:shadowRadius="0.0"/>
KeyBoard还有许多属性,需要添加的可以自行进行研究
2.在res文件中新建一个XML文件,写一个key.xml,就是你自定义键盘的布局
<?xml version="1.0" enCoding="utf-8"?><!--KeyHeight 表示每一个按键的高度 KeyWIDth:每一个按键2宽度5%--><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>
Row 表示一行,每一行中有三个key,表示一行中有三个按键。
里面的codes值是对应的ASCLL码值,即1对应的ASCLL码值为49.
对于我们常用的删除,清空这些按钮,在KeyBoard中也会有固定的codes,当然若是不知道也可以自己设置即可。
3.在Java文件中新建一个Utils包,里面写KeyBoardUtils.java,编写自定义键盘的逻辑属性
package com.example.bookkeePing.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.bookkeePing.R;public class KeyBoardUtils { private KeyboardVIEw keyboardVIEw; private EditText editText; private final Keyboard k1; //自定义键盘 public interface OnEnsureListener{ public voID onEnsure(); } OnEnsureListener onEnsureListener; public OnEnsureListener getonEnsureListener() { return 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);//取消d出系统键盘 k1= new Keyboard(this.editText.getContext(), R.xml.key); this.keyboardVIEw.setKeyboard(k1); //设置要显示键盘的样式 this.keyboardVIEw.setEnabled(true); this.keyboardVIEw.setPrevIEwEnabled(true); 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.getSelectionEnd(); 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); } }}
4.在要显示自定义键盘的界面(Java文件)中初始化界面,显示自定义键盘
package com.example.bookkeePing.frag_record;/** * 记录页面中的支出模块 */import androID.inputmethodservice.KeyboardVIEw;import androID.os.Bundle;import androIDx.fragment.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.EditText;import androID.Widget.GrIDVIEw;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import com.example.bookkeePing.R;import com.example.bookkeePing.utils.KeyBoardUtils;public class OutcomeFragment extends Fragment { KeyboardVIEw keyboardVIEw; EditText moneyEt; ImageVIEw typeIv; TextVIEw typeTv,beizhuTv,timeTv; GrIDVIEw typeGv; @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment VIEw vIEw= inflater.inflate(R.layout.fragment_outcome, container, false); initVIEw(vIEw); return vIEw; } private voID initVIEw(VIEw vIEw) { keyboardVIEw=vIEw.findVIEwByID(R.ID.frag_record_keyboard); moneyEt=vIEw.findVIEwByID(R.ID.frag_record_et_money); typeIv=vIEw.findVIEwByID(R.ID.frag_record_iv); beizhuTv=vIEw.findVIEwByID(R.ID.frag_record_tv_beizhu); timeTv=vIEw.findVIEwByID(R.ID.frag_record_tv_time); typeTv=vIEw.findVIEwByID(R.ID.frag_record_tv_type); typeGv=vIEw.findVIEwByID(R.ID.frag_record_gv); //让自定义软键盘显示出来 KeyBoardUtils boardUtils=new KeyBoardUtils(keyboardVIEw,moneyEt); boardUtils.showKeyboard(); //设置接口,监听确定按钮被点击了 boardUtils.setonEnsureListener(new KeyBoardUtils.OnEnsureListener() { @OverrIDe public voID onEnsure() { //点击了确定按钮 //获取记录的信息,保存在数据库中 //返回上一级页面 } }); }}
至此,自定义键盘就可以显示出来了,我做的自定义键盘界面显示如下图:
以上是内存溢出为你收集整理的Android 记账APP中写的一个自定义软键盘全部内容,希望文章能够帮你解决Android 记账APP中写的一个自定义软键盘所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)