兩種自定義安全鍵盤&屏蔽系統輸入法

本文主要講兩種自定義安全的鍵盤的實現,還有屏蔽系統輸入法!(尤其是如果設備上裝有谷歌中文輸入法,屏蔽谷歌輸入法【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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章