您的位置:首页 > 移动开发 > Android开发

Android 自定义键盘控件_身份证号码输入

2017-09-18 10:31 393 查看
效果图:



步骤:

1.继承键盘类,写一个MyKeyboardView ,备用。

public class MyKeyboardView extends KeyboardView {

private Context context;

public MyKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public void setContext(Context context) {
this.context = context;
}

@Override
public void onDraw(Canvas canvas) {
sup
4000
er.onDraw(canvas);
}
}


2.在res下新建一个xml文件夹,新建一个xml,用来描绘键盘的样式。

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0dp"
android:keyHeight="60dp"
android:keyWidth="25%p"
android:verticalGap="0dp">
<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="9997"
android:isRepeatable="true"
android:keyEdgeFlags="right"
android:keyHeight="180dp"
android:keyLabel="完成" />
</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:keyLabel="回删" />
<Key
android:codes="9995"
android:keyLabel="清除" />
</Row>
</Keyboard>


3.新建一个自定义控件类,真正实现键盘类的书写。

public class CustomKeyboard {

private final Context context;
private EditText mEdittext;
private MyKeyboardView mKeyboardView;
private Keyboard mKeyboard;

public CustomKeyboard(Context context, MyKeyboardView keyboardView, EditText editText) {
this.context = context;
this.mEdittext = editText;
mKeyboard = new Keyboard(context, R.xml.keyboard);
mKeyboardView = keyboardView;
mKeyboardView.setContext(context);
mKeyboardView.setKeyboard(mKeyboard);
mKeyboardView.setPreviewEnabled(false);
mKeyboardView.setOnKeyboardActionListener(actionListener);
}

private KeyboardView.OnKeyboardActionListener actionListener = new KeyboardView.OnKeyboardActionListener() {
@Override
public void onPress(int primaryCode) {
}

@Override
public void onRelease(int primaryCode) {

}

@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = mEdittext.getText();
int index = mEdittext.getSelectionStart();//光标位置
switch (primaryCode) {

case Keyboard.KEYCODE_DELETE://回退
if (editable != null && editable.length() > 0) {
if (index > 0) {
editable.delete(index - 1, index);
}
}
break;
case 9995://重输
mEdittext.setText("");
break;
case 9994://左移
if (index > 0) {
mEdittext.setSelection(index - 1);
}
break;
case 9996://右移
if (index < mEdittext.length()) {
mEdittext.setSelection(index + 1);
}
break;
case 9997://完成
hideKeyboard();
break;
default:
editable.insert(index, 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() {

}
};

public void showKeyboard() {
if (mKeyboardView.getVisibility() != View.VISIBLE) {
mKeyboardView.setVisibility(View.VISIBLE);
}
}

public void hideKeyboard() {
if (mKeyboardView.getVisibility() == View.VISIBLE) {
mKeyboardView.setVisibility(View.GONE);
}
}

public boolean isShowKeyboard() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}


4.在xml中加入键盘。

<com.ds.widget.MyKeyboardView
android:id="@+id/customKeyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#CBCBCB"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@drawable/bg_keyboard_btn"
android:keyTextColor="#000000"
android:keyTextSize="31dp"
android:labelTextSize="23.04sp"
android:shadowRadius="0"
android:visibility="gone" />


附加:为了让键盘拥有分隔的效果,需求给键盘添加keyBackground,代码如下:

//bg_keyboard_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:drawable="@drawable/keys_pressed_shape"/>
<item android:drawable="@drawable/keys_normal_shape"/>
</selector>


//keys_normal_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"/>
<gradient
android:startColor="#fff"
android:endColor="#eee"
android:angle="90"
/>
<stroke android:color="#CBCBCB"
android:dashWidth="2dip"
android:width="1dp"
/>
</shape>


//keys_pressed_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<gradient
android:angle="90"
android:endColor="#eee"
android:startColor="#fff" />
<stroke
android:width="1dp"
android:color="#eee"
android:dashWidth="2dip" />
</shape>


5.在java文件中添加相应的代码。

//1 屏蔽掉系统默认输入法
if (Build.VERSION.SDK_INT <= 10) {
cl_idcard.setInputType(InputType.TYPE_NULL);
} else {
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(cl_idcard, false);
} catch (Exception e) {
e.printStackTrace();
}
}
//2 初试化键盘
MyKeyboardView keyboardView = (MyKeyboardView) findViewById(R.id.customKeyboard);
customKeyboard = new CustomKeyboard(NoCardLiveActivity.this, keyboardView, cl_idcard);
if (cl_idcard.isSelected()) {
customKeyboard.showKeyboard();
} else customKeyboard.hideKeyboard();
cl_idcard.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
customKeyboard.showKeyboard();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(cl_idcard.getWindowToken(), 0);
return false;
}
});


以上为基本的代码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐