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

本文主要讲两种自定义安全的键盘的实现,还有屏蔽系统输入法!(尤其是如果设备上装有谷歌中文输入法,屏蔽谷歌输入法【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;
    }
在UiPath Studio中,自定义键盘快捷键来切换输入法并不是直接内置的功能。然而,你可以通过创建自定义活动(Custom Activities)或者使用脚本来模拟这个操作。通常情况下,你需要找到操作系统级别的API或者键盘事件处理,来触发输入法的切换。 以下是步骤概述: 1. **了解系统API**: 首先,需要查阅Windows API文档,找出与切换输入法相关的函数,如`SendInput`或`RegisterHotKey`等。 2. **编写脚本**: 使用UiPath的VB.NET或C#语言编写一个活动,该活动包含相应的API调用。例如,可以使用PowerShell或.NET代码来发送输入焦点切换到输入法控制台的命令。 ```csharp using System.Runtime.InteropServices; [Guid("YOUR_GUID")] // 自定义活动的GUID public class SwitchInputMethodActivity : ActivityBase { [Action] public void Execute() { // 替换为实际的API调用 SendInputMethodSwitchingCommand(); } [DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, uint id, uint fsModifiers, uint vk); // ... 其他必要的API函数和清理工作 } ``` 3. **添加到工具箱**: 将这个自定义活动添加到UiPath的工具箱中,以便在流程设计器里使用。 4. **配置快捷键**: 在流程设计中,右键点击新添加的活动并选择“属性”,然后设置自定义键盘快捷键。 5. **测试和部署**: 确保活动功能正常后,将其集成到你需要切换输入法的地方,并发布到生产环境中。 注意:这需要对底层技术和编程有一定了解,并可能涉及注册表更改或管理权限。此外,不同版本的UiPath可能会有不同的处理方式,最好参考官方文档或社区资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值