先看效果图:

1.在需要的调用软键盘的activity_mian.xml中加入键盘控件

<!--自定义键盘控件-->
<RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"><android.inputmethodservice.KeyboardViewandroid:id="@+id/keyboard_view"android:shadowRadius="0.0"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:focusable="true"android:focusableInTouchMode="true"android:keyPreviewLayout="@layout/key_preview_layout"android:background="#D6D9DE"android:keyBackground="@drawable/btn_keyboard_key"android:keyTextColor="#747474"android:keyTextSize="36dp"android:paddingTop="10dp"android:paddingBottom="10dp"android:paddingLeft="95dp"android:paddingRight="95dp"android:visibility="gone"/>
</RelativeLayout>
1.1
 
android:keyPreviewLayout="@layout/key_preview_layout"这个是长按键盘时弹出框的样式,如果不写,系统默认,默认的有时会会看不清
key_preview_layout.xml:
 
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="40sp"android:textColor="@android:color/white"android:gravity="center"android:background="#80929299"/>
1.2
android:keyBackground="@drawable/btn_keyboard_key"按键的形状
 
btn_keyboard_key.xml:
 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="#FFFFFFFF"/><corners android:topLeftRadius="8dip"android:topRightRadius="8dip"android:bottomRightRadius="8dip"android:bottomLeftRadius="8dip" />
</shape>

2.然后在res文件夹下新建文件夹,里面新建三个文件,即每种键盘样式的布局分别为qwerty.xml, symbols.xml, punctuate.xml

字母键盘-qwerty.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Keyboard android:keyWidth="100dp" android:keyHeight="60dp"android:horizontalGap="10dp" android:verticalGap="15dp"xmlns:android="http://schemas.android.com/apk/res/android"><Row><Key android:codes="113" android:keyEdgeFlags="left"android:keyLabel="q"  /><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:keyEdgeFlags="right"android:keyLabel="p" /></Row><Row><Key android:horizontalGap="4.999995%p" 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="110dp" android:codes="-1"android:keyEdgeFlags="left" android:isModifier="true"android:horizontalGap="33dp"android:isSticky="true" android:keyIcon="@drawable/shift" /><Key android:codes="122" android:keyLabel="z" android:horizontalGap="30dp"/><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="110dp" android:codes="-5"android:horizontalGap="30dp"android:keyEdgeFlags="right" android:isRepeatable="true"android:keyIcon="@drawable/delete" /></Row><Row android:rowEdgeFlags="bottom"><Key android:keyWidth="110dp" android:codes="-2" android:horizontalGap="43dp"android:keyIcon="@drawable/number" /><Key android:keyWidth="110dp" android:codes="46"android:keyIcon="@drawable/point" /><Key android:keyWidth="540dp" android:codes="32"android:isRepeatable="true" android:keyIcon="@drawable/space" /><Key android:keyWidth="110dp" android:codes="47"android:keyIcon="@drawable/gang"/><Key android:keyWidth="110dp" android:codes="-3"android:keyEdgeFlags="right" android:keyIcon="@drawable/complete" /></Row>
</Keyboard>

数字键盘-symbols.xml:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyWidth="100dp" android:horizontalGap="10dp"android:verticalGap="15dp" android:keyHeight="60dp"><Row><Key android:codes="49" android:keyEdgeFlags="left"android:keyLabel="1"  /><Key android:codes="50" android:keyLabel="2" /><Key android:codes="51" android:keyLabel="3" /><Key android:codes="52" android:keyLabel="4" /><Key android:codes="53" android:keyLabel="5" /><Key android:codes="54" android:keyLabel="6" /><Key android:codes="55" android:keyLabel="7" /><Key android:codes="55" android:keyLabel="8" /><Key android:codes="56" android:keyLabel="9" /><Key android:codes="48" android:keyEdgeFlags="right"android:keyLabel="0" /></Row><Row><Key android:codes="45" android:keyEdgeFlags="left"android:keyLabel="-"  /><Key android:codes="47" android:keyLabel="/" /><Key android:codes="65306" android:keyLabel=":" /><Key android:codes="65307" android:keyLabel=";" /><Key android:codes="65288" android:keyLabel="(" /><Key android:codes="65289" android:keyLabel=")" /><Key android:codes="37" android:keyLabel="%" /><Key android:codes="64" android:keyLabel="\@" /><Key android:codes="8220" android:keyLabel="“" /><Key android:codes="8221" android:keyEdgeFlags="right"android:keyLabel="”" /></Row><Row><Key android:keyWidth="110dp" android:codes="-7"android:keyEdgeFlags="left" android:isModifier="true" android:horizontalGap="90dp"android:isSticky="true" android:keyIcon="@drawable/punctuate" /><Key android:codes="12290" android:keyLabel="。" android:horizontalGap="30dp"/><Key android:codes="65292" android:keyLabel="," /><Key android:codes="12289" android:keyLabel="、" /><Key android:codes="65311" android:keyLabel="?" /><Key android:codes="65281" android:keyLabel="!" /><Key android:codes="46" android:keyLabel="." /><Key android:keyWidth="110dp" android:codes="-5"android:horizontalGap="30dp"android:keyEdgeFlags="right" android:isRepeatable="true"android:keyIcon="@drawable/delete" /></Row><Row android:rowEdgeFlags="bottom"><Key android:keyWidth="110dp" android:codes="-2" android:horizontalGap="90dp"android:keyIcon="@drawable/pinyin" /><Key android:keyWidth="650dp" android:codes="32" android:horizontalGap="30dp"android:isRepeatable="true" android:keyIcon="@drawable/space" /><Key android:keyWidth="110dp" android:codes="-3" android:horizontalGap="30dp"android:keyEdgeFlags="right" android:keyIcon="@drawable/complete" /></Row></Keyboard>

标点键盘-punctuate.xml

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyWidth="100dp" android:horizontalGap="17dp"android:verticalGap="15dp" android:keyHeight="60dp"><Row><Key android:codes="12304" android:keyEdgeFlags="left"android:keyLabel="【"  android:horizontalGap="50dp"/><Key android:codes="12305" android:keyLabel="】" /><Key android:codes="65371" android:keyLabel="{" /><Key android:codes="65373" android:keyLabel="}" /><Key android:codes="35" android:keyLabel="#" /><Key android:codes="94" android:keyLabel="^" /><Key android:codes="42" android:keyLabel="*" /><Key android:codes="43" android:keyLabel="+" /><Key android:codes="61" android:keyEdgeFlags="right"android:keyLabel="=" /></Row><Row><Key android:codes="45" android:keyEdgeFlags="left"android:keyLabel="-"  android:horizontalGap="50dp"/><Key android:codes="8212" android:keyLabel="—" /><Key android:codes="92" android:keyLabel="\\" /><Key android:codes="124" android:keyLabel="|" /><Key android:codes="126" android:keyLabel="~" /><Key android:codes="12298" android:keyLabel="《" /><Key android:codes="12299" android:keyLabel="》" /><Key android:codes="38" android:keyLabel="&"/><Key android:codes="65509" android:keyEdgeFlags="right"android:keyLabel="¥" /></Row><Row><Key android:keyWidth="110dp" android:codes="-7"android:keyEdgeFlags="left" android:isModifier="true"android:horizontalGap="80dp"android:isSticky="true" android:keyIcon="@drawable/number" /><Key android:codes="8230" android:keyLabel="…" android:horizontalGap="30dp"/><Key android:codes="65509" android:keyLabel="¥" /><Key android:codes="12289" android:keyLabel="、" /><Key android:codes="65311" android:keyLabel="?" /><Key android:codes="8216" android:keyLabel="‘" /><Key android:codes="8217" android:keyLabel="’" /><Key android:keyWidth="110dp" android:codes="-5"android:horizontalGap="30dp"android:keyEdgeFlags="right" android:isRepeatable="true"android:keyIcon="@drawable/delete" /></Row><Row android:rowEdgeFlags="bottom"><Key android:keyWidth="110dp" android:codes="-2" android:horizontalGap="80dp"android:keyIcon="@drawable/pinyin" /><Key android:keyWidth="685dp" android:codes="32" android:horizontalGap="30dp"android:isRepeatable="true" android:keyIcon="@drawable/space" /><Key android:keyWidth="110dp" android:codes="-3" android:horizontalGap="30dp"android:keyEdgeFlags="right" android:keyIcon="@drawable/complete" /></Row></Keyboard>
android:keyIcon="@drawable/space" 的部分没有图片的话可以换成 android:keyLabel=" "括号里用相应字母或符号代替

3.然后就是KeyBoardUtil工具类,网上有很多内容大同小异,我为了实现三种键盘的切换进行了一些改动

KeyBoardUtil.java

package com.dcdz.app.base;import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;import com.dcdz.app.R;import java.util.List;public class KeyboardUtil {private Context ctx;private Activity act;private KeyboardView keyboardView;private Keyboard k1;// 字母键盘private Keyboard k2;// 数字键盘private Keyboard k3;//标点符号键盘public boolean isnun = false;// 是否数据键盘public boolean ispun = false;//是否标点键盘public boolean isupper = false;// 是否大写public static final int KEYCODE_PUN = -7;private EditText ed;public KeyboardUtil(Activity act, Context ctx, EditText edit) {this.act = act;this.ctx = ctx;this.ed = edit;k1 = new Keyboard(ctx, R.xml.qwerty);k2 = new Keyboard(ctx, R.xml.symbols);k3 = new Keyboard(ctx,R.xml.punctuate);keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);keyboardView.setKeyboard(k1);keyboardView.setEnabled(true);keyboardView.setPreviewEnabled(true);keyboardView.setOnKeyboardActionListener(listener);
}private OnKeyboardActionListener listener = new OnKeyboardActionListener() {@Overridepublic void swipeUp() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeDown() {}@Overridepublic void onText(CharSequence text) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onPress(int primaryCode) {}@Overridepublic void onKey(int primaryCode, int[] keyCodes) {Editable editable = ed.getText();int start = ed.getSelectionStart();if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成hideKeyboard();} 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) {// 大小写切换changeKey();keyboardView.setKeyboard(k1);} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换if (isnun) {isnun = false;keyboardView.setKeyboard(k1);} else {isnun = true;keyboardView.setKeyboard(k2);}}else if(primaryCode == KEYCODE_PUN){if(ispun){ispun = false;keyboardView.setKeyboard(k2);}else {ispun = true;keyboardView.setKeyboard(k3);}}else if (primaryCode == 57419) { // go leftif (start > 0) {ed.setSelection(start - 1);}} else if (primaryCode == 57421) { // go rightif (start < ed.length()) {ed.setSelection(start + 1);}} else {editable.insert(start, Character.toString((char) primaryCode));}}};/*** 键盘大小写切换*/private void changeKey() {List<Key> keylist = k1.getKeys();if (isupper) {//大写切换小写isupper = false;for(Key key:keylist){if (key.label!=null && isword(key.label.toString())) {key.label = key.label.toString().toLowerCase();key.codes[0] = key.codes[0]+32;}}} else {//小写切换大写isupper = true;for(Key key:keylist){if (key.label!=null && isword(key.label.toString())) {key.label = key.label.toString().toUpperCase();key.codes[0] = key.codes[0]-32;}}}}public void showKeyboard() {int visibility = keyboardView.getVisibility();if (visibility == View.GONE || visibility == View.INVISIBLE) {keyboardView.setVisibility(View.VISIBLE);}}public void hideKeyboard() {int visibility = keyboardView.getVisibility();if (visibility == View.VISIBLE) {keyboardView.setVisibility(View.INVISIBLE);}}private boolean isword(String str){String wordstr = "abcdefghijklmnopqrstuvwxyz";if (wordstr.indexOf(str.toLowerCase())>-1) {return true;}return false;}}

4.最后是调用

4.1在Fragment里调

一般的项目都是在Fragment里调的,这个根据你的文本框的位置具体来定吧

我这边内容较多,只写关键代码

public class UserLoginFragment extends SupportFragment {FragmentFingerUserloginBinding binding;private EditText etUserName;private EditText etPassword;private Context ctx;private Activity act;private KeyboardView keyboardView;public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {binding = DataBindingUtil.inflate(inflater, R.layout.fragment_finger_userlogin, container, false);act = getActivity();ctx = getActivity().getBaseContext();keyboardView = (KeyboardView) getActivity().findViewById(R.id.keyboard_view);etUserName = binding.etUsername;etPassword = binding.etPassword;initView();return binding.getRoot();}private void initView() {if (binding != null) {etUserName.requestFocus();
//            etUserName.setFocusableInTouchMode(true);etUserName.addTextChangedListener(watcher);etPassword.addTextChangedListener(watcher);etUserName.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);etPassword.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);//调用自定义键盘etUserName.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {keyboardView.setKeyboard(k1);
//                    int inputback = etUserName.getInputType();
//                    etUserName.setInputType(InputType.TYPE_NULL);new KeyboardUtil(act,act.getBaseContext(), etUserName).showKeyboard();
//                    etUserName.setInputType(inputback);return false;}});etPassword.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {keyboardView.setKeyboard(k2);new KeyboardUtil(act, act.getBaseContext(), etPassword).showKeyboard();return false;}});}}}
 

4.2在Activity里调,在Activity里调我是为了在这里实现触摸屏幕其他位置隐藏软键盘

关键代码

public class FingerActivity extends SupportActivity {private KeyboardView keyboardView;@SuppressLint("ResourceType")protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = DataBindingUtil.setContentView(this, R.layout.activity_finger);keyboardView = (KeyboardView) findViewById(R.id.keyboard_view);initView();loadRootFragment(new UserLoginFragment());}
 
public boolean onTouchEvent(MotionEvent event) {if(null != this.getCurrentFocus()){/*** 点击空白位置 隐藏软键盘*/keyboardView.setVisibility(View.INVISIBLE);}return super .onTouchEvent(event);}

5到这里即基本可以实现了,在使用的时候肯定还会遇到很多小问题,这里也不一 一列举了。

 
 
 
 
 

Android自定义软键盘样式:字母、数字、标点三种切换相关推荐

  1. android自定义系统键盘,Android自定义软键盘

    [实例简介] Android自定义软键盘 [实例截图] [核心代码] keydemo └── keydemo ├── AndroidManifest.xml ├── bin │   ├── class ...

  2. Android 自定义软键盘实现 数字九宫格

    前言 最近项目在对接美团外卖功能 实现外面小哥凭取货码取货 对接完功能后 用户反馈 弹出的软键盘 很难输入 数字太小了 大概是下面这种显示方式 需求 组长说 要不搞一个自定义软键盘吧 数字搞大点 方便 ...

  3. Android自定义软键盘输入法,隐藏系统输入法显示光标的实现

    android实现自定义软键盘,先上图看效果,效果基本上是仿ios输入法实现的 这里是实现隐藏系统输入法,同时让EditText能获取光标的代码部分(通过反射调用): <span style=& ...

  4. Android 自定义软键盘实现 数字九宫格,2021年Android常见面试题目

    实现效果GIF 实现代码 自定义View 一个NineNumericKeyboardView /** Author by Lyu Date on 2021/5/26-19:55 Description ...

  5. android自动软键盘,Android自定义软键盘

    MyKeyboard Android自定义键盘的使用 实现步骤 第一步: 1.新建一个xml文件夹放在res目录下面,然后新建xml文件:money_keyboard.xml 2.然后在XML文件中添 ...

  6. Android 自定义软键盘实现

    module链接:https://download.csdn.net/download/meixi_android/10652565 compile project(':edlibrary') , ' ...

  7. android listview 自定义控件,Android 自定义弹性ListView控件实例代码(三种方法)

    关于在Android中实现ListView的弹性效果,有很多不同的方法,网上一搜,也有很多,下面贴出在项目中经常用到的两种实现ListView弹性效果的方法(基本上拿来就可以用),供大家参考: 弹性L ...

  8. android自定义键盘开源,Android自定义软键盘的设计与实现代码

    偶然间发现了Android.inputmethodservice.Keyboard类,即android可以自定义键盘类,做了一个简单例子供大家参考. 效果如下: 先看界面布局文件 android:la ...

  9. Android自定义软键盘的实现

    先看界面布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

最新文章

  1. 在腾讯,如何做 Code Review?
  2. golang中的os包
  3. [20180102]11g的V$SORT_USAGE视图.txt
  4. [Swift]LeetCode873. 最长的斐波那契子序列的长度 | Length of Longest Fibonacci Subsequence...
  5. JQuery实现动态插入HTML模块
  6. 由浅入深CIL系列:5.抛砖引玉:判断string是否为空的四种方法的CIL代码看看效率如何?...
  7. LeetCode 1474. 删除链表 M 个节点之后的 N 个节点
  8. base64 pdf解码无法打开_微信小程序使用echarts导出PDF
  9. DELL MD3200I存储单双控制器的自由切换
  10. 我们去庐山玩啦,公司组织,带上老婆
  11. Mutable和Immutable
  12. Http请求报文解析
  13. iOS 越狱检测/反越狱
  14. excel怎么一个格子斜分_表格excel怎样把一格用斜线分为三格
  15. 理解计算-从根号 2 到 AlphaGo
  16. ajax hack,Ajax Hacks-hack9 深入了解HTTP Respon_jquery
  17. 组合体视图的画图步骤_§5-2 画组合体视图的方法与步骤—叠加型组合体的视图画法...
  18. 录屏工具ScreenToGif功能总结
  19. 解决Word文章表格中无法自动换页的问题,表格结尾处像是被下一页覆盖掉了
  20. shell命令之cd切换目录

热门文章

  1. Git 15周年:当年的分道扬镳,成就了今天的开源传奇
  2. java设计一个秒杀系统_面试宝典系列-怎么设计一个秒杀系统
  3. 工业绿色环保发展:打造风电场远程集控中心可视化系统
  4. 【2018国赛线上初赛】知识问答题真题演练第三波
  5. python海伦公式_「海伦公式」海伦公式: - seo实验室
  6. 做一款热门游戏----没有99美元的Impact也行
  7. 盛大创新院赞助首届.NET技术交流会 - 演讲录像及下载
  8. mysql配置修改_mysql配置修改
  9. 什么叫机械硬盘_西部数据固态硬盘和机械硬盘的区别是什么
  10. 数字冰雹 数字孪生城市智能运营中心(IOC)可视化决策系统