您的位置:首页 > 其它

两种自定义安全键盘&屏蔽系统输入法

2017-02-17 10:23 387 查看
本文主要讲两种自定义安全的键盘的实现,还有屏蔽系统输入法!(尤其是如果设备上装有谷歌中文输入法,屏蔽谷歌输入法【4.0版本以上】失效的情况)

目录

目录

第一种自定义键盘实现

第二种自定义键盘实现

屏蔽系统输入法

第一种自定义键盘实现

自定义一个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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐