两种自定义安全键盘&屏蔽系统输入法
2017-02-17 10:23
387 查看
本文主要讲两种自定义安全的键盘的实现,还有屏蔽系统输入法!(尤其是如果设备上装有谷歌中文输入法,屏蔽谷歌输入法【4.0版本以上】失效的情况)
第一种自定义键盘实现
第二种自定义键盘实现
屏蔽系统输入法
在java代码中,find该keyboardview,
symbols_num.xml文件如下:
原生的keyboardview的每种按键风格一样,不太适合大多数人的需求。
所以需要自定义keyboardview,在ondraw()方法里,对相应的按键做处理。
例如:codes=”-5”删除按键的按键风格不同于其他的按键,可以把上文的keyboard传过来,然后通过以下方法获取键值,将key的code=-5的删除键设置相应的按键风格
如果使用第二种键盘的话,就不会出现这种问题。
或者你可以:
卸载谷歌输入法
不要使用谷歌输入法,换成其他的输入法
谷歌输入法不要升级到4版本以上
如果你非要执着使用第一种键盘,并且还使用4版本以上的谷歌输入法的话。那么——-
目录
目录第一种自定义键盘实现
第二种自定义键盘实现
屏蔽系统输入法
第一种自定义键盘实现
自定义一个view继承ViewGroup,ViewGroup里面放键盘对应的按键button,给各个button设置点击事件。 例如:给数字按键1设置点击事件。
Button button01=new Button(getContext()); button01.setOnClickListener(new OnClickListener){ @Override public void onClick(View v){ Instrumentation inst=new Instrumentation(); inst.sendKeyDownUpSync(KeyEvent.KEYCODE_1); } };
第二种自定义键盘实现
使用KeyBoard这个类,需要放置小键盘的时候,在xml布局中书写keyboardview,<android.inputmethodservice.KeyboardView android:id="@+id/keyboard_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:focusable="true" android:focusableInTouchMode="true" android:keyBackground="@drawablel/btn_keyboard_key" android:keyTextColor="@color/white" android:vissibility="gone"/>
在java代码中,find该keyboardview,
Keyboard numKeyboard=new Keyboard(getContext(),R.xml.symbols_num); keyboardView.setKeyboard(numKeyboard);
symbols_num.xml文件如下:
<-- symbols_num.xml--> <?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:horizontalGap="0px" android:keyHeight="8.5%p" android:keyWidth="33.333333%p" android:verticalGap="0px" > <Row> <Key android:codes="49" android:keyLabel="1" /> <Key android:codes="50" android:keyLabel="2" /> <Key android:codes="51" android:keyLabel="3" /> </Row> <Row> <Key android:codes="52" android:keyLabel="4" /> <Key android:codes="53" android:keyLabel="5" /> <Key android:codes="54" android:keyLabel="6" /> </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" /> <Key android:codes="48" android:keyLabel="0" /> <Key android:codes="-5" android:isRepeatable="true" /> </Row> </Keyboard>
原生的keyboardview的每种按键风格一样,不太适合大多数人的需求。
所以需要自定义keyboardview,在ondraw()方法里,对相应的按键做处理。
例如:codes=”-5”删除按键的按键风格不同于其他的按键,可以把上文的keyboard传过来,然后通过以下方法获取键值,将key的code=-5的删除键设置相应的按键风格
List<Key> keys =mKeyBoard.getKeys();
屏蔽系统输入法
以下方法可以屏蔽系统输入法,但是如果使用第一种自定义键盘的话,是不能屏蔽谷歌中文输入法的。不过如果给edittext的inputType设置了含有number的Type是不会弹出谷歌中文输入法的。如果使用第二种键盘的话,就不会出现这种问题。
或者你可以:
卸载谷歌输入法
不要使用谷歌输入法,换成其他的输入法
谷歌输入法不要升级到4版本以上
如果你非要执着使用第一种键盘,并且还使用4版本以上的谷歌输入法的话。那么——-
/*** * * * @param edit * @return 判断输入法是否打开 */ public boolean isKeyBoardOpen(EditText edit) { boolean flag = false; InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE); // isOpen若返回true,则表示输入法打开 boolean isOpen = imm.isActive(); if (isOpen) { if (imm.hideSoftInputFromWindow(edit.getWindowToken(), 0)) flag = true; } int currentVersion = android.os.Build.VERSION.SDK_INT; String methodName = null; if (currentVersion >= 16) { // 4.2 methodName = "setShowSoftInputOnFocus"; } else if (currentVersion >= 14) { // 4.0 methodName = "setSoftInputShownOnFocus"; } if (methodName == null) { edit.setInputType(InputType.TYPE_NULL); } else { Class<EditText> cls = EditText.class; Method setShowSoftInputOnFocus; try { setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class); setShowSoftInputOnFocus.setAccessible(true); setShowSoftInputOnFocus.invoke(edit, false); } catch (NoSuchMethodException e) { edit.setInputType(InputType.TYPE_NULL); e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } return flag; }
相关文章推荐
- Android 强行夺取EditText系统键盘输入改为自定义安全键盘输入
- 两种自定义系统弹出键盘上方的view
- 两种自定义系统弹出键盘上方的view
- android安卓屏蔽禁用系统输入法,自定义软键盘,解决EditText光标问题demo
- 关于Android外设键盘导致系统输入法隐藏的解决方案
- iOS系统键盘和自定义键盘的切换
- 20145221高其&20145326蔡馨熠《信息安全系统设计基础》实验三 实时系统的移植
- <寒江独钓>Windows内核安全编程__键盘过滤之内核级Hook(一)
- 信息安全系统设计基础第三次实验报告 20135324&&20135330
- 为了系统的安全Linux中如何屏蔽storm ui的kill功能
- IOS开发之自定义系统弹出键盘上方的view
- android 输入法横屏下禁止全屏&调整横屏模式下键盘高度
- Android自定义安全键盘
- OpenWrt系统安全改进<二> --- 使能PAM
- Android安全输入设计与思考,android设计思考 为什么使用安全键盘? 安全的输入 各大公司的安全键盘设计 开始自定义安全键盘 安全键盘还需要注意的
- Android系统移植与调试之------->如何修改开机动画的两种方式剖析
- EditText实现点击屏蔽软键盘并展示自定义键盘
- 20145221高其&20145326蔡馨熠《信息安全系统设计基础》实验四 外设驱动程序设计
- 自定义leftBarbuttonItem时,左滑手势会失效,解决左边滑动手势失效方法 ,  怎么停止系统的手势
- TV中一款自定义的软键盘(覆盖了系统原生的键盘)