身份证键盘

第一步

在xml文件夹下创建idcard_keyboard.xml

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="2dp"
android:verticalGap="2dp"
android:keyHeight="60dp"
android:keyWidth="33%p">
<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="88"
        android:keyLabel="X"
        />
    <Key
        android:codes="48"
        android:keyLabel="0" />
    <Key
        android:codes="-5"
        android:isRepeatable="true"
        android:keyIcon="@drawable/key_board_delete_one"/>
</Row>
</Keyboard>

第二步

在value文件夹下的attrs.xml中添加

<declare-styleable name="Keyboard">
    <attr name="xml" format="reference" />
</declare-styleable>

第三步

创建keyboard_view.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e6e6e6"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@color/white"
android:keyTextColor="#333333"
android:keyTextSize="31dp"
android:labelTextSize="23.04sp" />

第四步

创建CustomKeyboardEditText

public class CustomKeyboardEditText extends android.support.v7.widget.AppCompatEditText implements  KeyboardView.OnKeyboardActionListener,
    View.OnClickListener {
private Keyboard mKeyboard;
private KeyboardView mKeyboardView;
private PopupWindow mKeyboardWindow;
private View mDecorView;

public CustomKeyboardEditText(Context context) {
    this(context, null);
}

public CustomKeyboardEditText(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomKeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initKeyboardView(context, attrs);
}

private void initKeyboardView(Context context, AttributeSet attrs) {
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Keyboard);
    if (!array.hasValue(R.styleable.Keyboard_xml)) {
        throw new IllegalArgumentException("you need add keyboard_xml argument!");
    }
    int xmlId = array.getResourceId(R.styleable.Keyboard_xml, 0);
    mKeyboard = new Keyboard(context, xmlId);
    mKeyboardView = (KeyboardView) LayoutInflater.from(context).inflate(R.layout.keyboard_view, null);
    //键盘关联keyboard对象
    mKeyboardView.setKeyboard(mKeyboard);
    //关闭键盘按键预览效果,如果按键过小可能会比较适用。
    mKeyboardView.setPreviewEnabled(false);
    //设置键盘事件
    mKeyboardView.setOnKeyboardActionListener(this);
    //将keyboardview放入popupwindow方便显示以及位置调整。
    mKeyboardWindow = new PopupWindow(mKeyboardView,
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    array.recycle();
    //设置点击事件,点击后键盘弹起,系统键盘收起。
    setOnClickListener(this);
    //屏蔽当前edittext的系统键盘
    notSystemSoftInput();
}

@Override
public void onPress(int primaryCode) {

}

@Override
public void onRelease(int primaryCode) {

}

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    Editable editable = this.getText();
    //获取光标偏移量下标
    int startIndex = this.getSelectionStart();
    switch (primaryCode) {
        case Keyboard.KEYCODE_CANCEL:// 隐藏键盘
            hideKeyboard();
            break;
        case Keyboard.KEYCODE_DELETE:// 回退
            if (editable != null && editable.length() > 0) {
                if (startIndex > 0) {
                    editable.delete(startIndex - 1, startIndex);
                }
            }
            break;
        case 9994://左移
            setSelection(startIndex - 1);
            break;
        case 9995://重输
            editable.clear();
            break;
        case 9996://右移
            if (startIndex < length()) {
                setSelection(startIndex + 1);
            }
            break;
        default:
            editable.insert(startIndex, Character.toString((char) primaryCode));
            break;
    }
}

@Override
public void onText(CharSequence text) {

}

@Override
public void swipeLeft() {

}

@Override
public void swipeRight() {

}

@Override
public void swipeDown() {

}

@Override
public void swipeUp() {

}

/**
 * 根据key code 获取 Keyboard.Key 对象
 *
 * @param primaryCode
 * @return
 */
private Keyboard.Key getKeyByKeyCode(int primaryCode) {
    if (null != mKeyboard) {
        List<Keyboard.Key> keyList = mKeyboard.getKeys();
        for (int i = 0, size = keyList.size(); i < size; i++) {
            Keyboard.Key key = keyList.get(i);

            int codes[] = key.codes;

            if (codes[0] == primaryCode) {
                return key;
            }
        }
    }

    return null;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (null != mKeyboardWindow) {
            if (mKeyboardWindow.isShowing()) {
                mKeyboardWindow.dismiss();
                return true;
            }
        }
    }
    return super.onKeyDown(keyCode, event);
}

@SuppressLint("MissingSuperCall")
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    if (!focused) {
        hideKeyboard();
    } else {
        hideSysInput();
        showKeyboard();
    }
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.mDecorView = ((Activity) getContext()).getWindow().getDecorView();
    hideSysInput();
}

@Override
public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    hideKeyboard();
    mKeyboardWindow = null;
    mKeyboardView = null;
    mKeyboard = null;
    mDecorView = null;
}

/**
 * 显示自定义键盘
 */
private void showKeyboard() {
    if (null != mKeyboardWindow) {
        if (!mKeyboardWindow.isShowing()) {
            mKeyboardView.setKeyboard(mKeyboard);
            mKeyboardWindow.showAtLocation(this.mDecorView, Gravity.BOTTOM, 0, 0);
        }
    }
}

/**
 * 屏蔽系统输入法
 */
private void notSystemSoftInput() {
    if (Build.VERSION.SDK_INT <= 10) {
        setInputType(InputType.TYPE_NULL);
    } else {
        ((Activity) getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        try {
            Class<EditText> cls = EditText.class;
            Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            setShowSoftInputOnFocus.setAccessible(true);
            setShowSoftInputOnFocus.invoke(this, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/**
 * 隐藏自定义键盘
 */
private void hideKeyboard() {
    if (null != mKeyboardWindow) {
        if (mKeyboardWindow.isShowing()) {
            mKeyboardWindow.dismiss();
        }
    }
}

/**
 * 隐藏系统键盘
 */
private void hideSysInput() {
    if (this.getWindowToken() != null) {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(this.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

@Override
public void onClick(View v) {
    requestFocus();
    requestFocusFromTouch();
    hideSysInput();
    showKeyboard();
}
}

第四步在布局文件中使用

    <com.nirvana.ylmc.widget.CustomKeyboardEditText
        android:id="@+id/identify_card_et"
        android:layout_width="@dimen/ds590"
        android:layout_height="@dimen/ds89"
        android:layout_marginLeft="@dimen/ds37"
        android:background="@drawable/new_edit_bg"
        android:gravity="center_vertical"
        android:hint="您的身份证号"
        app:xml="@xml/idcard_keyboard"
        android:paddingLeft="@dimen/ds33"
        android:textColor="#333333"
        android:textColorHint="#D8D8D8"
        android:textSize="@dimen/ds31" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值