標籤:
在做p2p理財項目,有些介面避免有校社會安全號碼碼及購買數量的輸入,所以採取自訂軟體盤的方式來實現更好的輸入體驗.
那麼怎麼彈出和隱藏自己自訂的軟鍵盤呢?關鍵代碼如下
if (SDK_INT <= 10) { // 屏蔽預設IME edText.setInputType(InputType.TYPE_NULL); } else { //反射的方法實現避免彈出系統內建的軟鍵盤 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); /**方法一*/ try { Class<EditText> cls = EditText.class; java.lang.reflect.Method setShowSoftInputOnFocus; if (SDK_INT >= 16) { // 4.2 setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class); } else { // 4.0 setShowSoftInputOnFocus = cls.getMethod("setSoftInputShownOnFocus", boolean.class); } setShowSoftInputOnFocus.setAccessible(true); setShowSoftInputOnFocus.invoke(edText, false); } catch (Exception e) { e.printStackTrace(); } }
利用JAVA反射調用 EditText 的私人方法,來避免彈出軟鍵盤 不同android版本的SDK 方法明不一樣,這樣就成功的避免了系統的軟體盤的彈出
給EditText添加相關的事件如:
edText.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (keyboardUtil == null) { keyboardUtil = new KeyboardUtil(MainActivity.this, MainActivity.this, num); keyboardUtil.setMyOnkeys(myOnkeys); } keyboardUtil.setEditText(edText); switch (edText.getId()) { case R.id.edit: keyboardUtil.displayleftAndright(); break; case R.id.edit_num: keyboardUtil.displayleftAndrightNum(num); break; } keyboardUtil.showKeyboard(bottomView); } });
如果一個介面有多個EditText 我們還需要監聽焦點擷取的事件設定當前需要輸入的EditText
edText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean b) { if (b) { if (keyboardUtil != null) { //設定當前擷取的EDITTEXT keyboardUtil.setEditText(edText); switch (edText.getId()) { case R.id.edit: keyboardUtil.displayleftAndright(); break; case R.id.edit_num: keyboardUtil.displayleftAndrightNum(num); break; } } } } });
必須監聽activity的back鍵,以免軟鍵盤彈出後按back導致activity被銷毀
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && keyboardUtil != null && keyboardUtil.hideKeyboard(bottomView)) { return false; } return super.onKeyDown(keyCode, event); }
接下來我們如何?自訂軟鍵盤
安卓本身就提供了android.inputmethodservice.KeyboardView類和Keyboard 來顯示自訂軟鍵盤, 項目中我只用到了數字鍵的輸入接下來實現的是數字鍵台
public KeyboardUtil(Activity act, Context ctx) {k2 = new Keyboard(ctx, R.xml.symbols);keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);keyboardView.setKeyboard(k2);keyboardView.setEnabled(true);keyboardView.setPreviewEnabled(false);keyboardView.setOnKeyboardActionListener(listener);}
首先自訂一個鍵盤的布局XML
<?xml version="1.0" encoding="utf-8"?><Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyWidth="25%p" android:horizontalGap="0px"android:verticalGap="0px" android:keyHeight="@dimen/key_height"><Row><Key android:codes="49" android:keyLabel="1" /><Key android:codes="50" android:keyLabel="2" /><Key android:codes="51" android:keyLabel="3" /><Key android:codes="57419" android:keyEdgeFlags="left"android:keyIcon="@drawable/sym_keyboard_left" /></Row><Row><Key android:codes="52" android:keyLabel="4" /><Key android:codes="53" android:keyLabel="5" /><Key android:codes="54" android:keyLabel="6" /><Key android:codes="57421" android:keyEdgeFlags="right"android:keyIcon="@drawable/sym_keyboard_right" /></Row><Row><Key android:codes="55" android:keyLabel="7" /><Key android:codes="56" android:keyLabel="8" /><Key android:codes="57" android:keyLabel="9" /><Key android:codes="-3" android:keyHeight="100dip"android:keyEdgeFlags="right" android:isRepeatable="true"android:keyLabel="完成" /></Row><Row><Key android:codes="-2" android:keyLabel="X" /><Key android:codes="48" android:keyLabel="0" /><Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete" /></Row></Keyboard>
介面顯示後的效果如下:
有些身份證後面數字帶有"x",鍵盤中內建"x",省略掉鍵盤的切換
輸入數量時的鍵盤的介面顯示
項目中輸入數量都是一個整數,點擊數字這樣就可以直接方便的修改數量
當然也可以實現其它樣式的軟鍵盤,實現的原理都是一樣的,根據自自己的項目需求來自訂鍵盤。
關鍵實現軟鍵盤的類在這裡不做過多的描述直接下載源碼。
源碼:https://github.com/MikeJon/Keyboard
android 中自訂軟體盤用於特需介面的輸入