标签:
在做p2p理财项目,有些界面避免有校身份证号码及购买数量的输入,所以采取自定义软件盘的方式来实现更好的输入体验.
那么怎么弹出和隐藏自己自定义的软键盘呢?关键代码如下
if (SDK_INT <= 10) { // 屏蔽默认输入法 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(); } }
给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); } });
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; } } } } });
@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
标签:
原文地址:http://blog.csdn.net/wuchuy/article/details/51362379