使用:
new KeyBoardUtil(activity, editText);
CarKeyboardView.java
public class CarKeyboardView extends KeyboardView {
public CarKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public CarKeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Keyboard keyboard = getKeyboard();
if (keyboard == null) return;
List<Keyboard.Key> keys = keyboard.getKeys();
if (keys != null && keys.size() > 0) {
@SuppressLint("DrawAllocation")
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
paint.setTypeface(font);
paint.setAntiAlias(true);
for (Keyboard.Key key : keys) {
if (key.codes[0] == -4) { // 下一步
Drawable dr = getContext().getResources().getDrawable(R.mipmap.key_next);
dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
dr.draw(canvas);
} else if (key.codes[0] == -1) { // 上一步
Drawable dr = getContext().getResources().getDrawable(R.mipmap.key_last);
dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
dr.draw(canvas);
} else if (key.codes[0] == -5) { // 删除
Drawable dr = getContext().getResources().getDrawable(R.mipmap.key_dele);
dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
dr.draw(canvas);
}
}
}
}
}
KeyBoardUtil.java
public class KeyBoardUtil {
/**
* 显示键盘的视图
*/
private Activity mActivity;
/**
* 键盘视图
*/
private KeyboardView mKeyboardView;
/**
* 键盘
*/
private Keyboard mKeyboard;
/**
* 输入框
*/
private EditText mEditText;
/**
* 键盘布局
*/
private View mViewContainer;
/**
* 焦点改变监听
*/
View.OnFocusChangeListener mOnFocusChangeListener = new View.OnFocusChangeListener() {
@Override public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) showSoftKeyboard();
else hideSoftKeyboard();
}
};
/**
* 构造方法
* @param activity 根视图
*/
public KeyBoardUtil(Activity activity , EditText editText){
this.mActivity = activity;
this.mKeyboard = new Keyboard(mActivity, R.xml.num_key);
attachTo(editText , true);
}
/**
* 绑定输入框
* 注意: 需要在父布局添加下面属性方可正常使用
* android:focusable="true"
* android:focusableInTouchMode="true"
* @param editText 输入框
* @param isAuto 是否自动显示
*/
public void attachTo(EditText editText, boolean isAuto){
this.mEditText = editText;
mEditText.setFocusable(true);
mEditText.setFocusableInTouchMode(true);
hideSystemSoftKeyboard(this.mEditText);
setAutoShowOnFocus(isAuto);
}
/**
* 隐藏系统软件盘
* @param editText 输入框
*/
@TargetApi(Build.VERSION_CODES.KITKAT) private void hideSystemSoftKeyboard(EditText editText) {
int sdkInt = Build.VERSION.SDK_INT;
if (sdkInt < 11){
editText.setInputType(InputType.TYPE_NULL);
} else {
try {
Class<EditText> cls = EditText.class;
Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setShowSoftInputOnFocus.setAccessible(true);
setShowSoftInputOnFocus.invoke(editText, false);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 焦点时自动显示
* @param enabled 是否显示
*/
private void setAutoShowOnFocus(boolean enabled){
if (null == mEditText) return;
if (enabled) mEditText.setOnFocusChangeListener(mOnFocusChangeListener);
else mEditText.setOnFocusChangeListener(null);
}
/**
* 显示软键盘
*/
public void showSoftKeyboard() {
if (null == mViewContainer) {
mViewContainer = mActivity.getLayoutInflater().inflate(R.layout.num_layout, null);
mViewContainer.findViewById(R.id.key_pack_up).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideSoftKeyboard();
}
});
} else {
if (null != mViewContainer.getParent()) return;
}
FrameLayout frameLayout = (FrameLayout) mActivity.getWindow().getDecorView();
KeyboardView keyboardView = mViewContainer.findViewById(R.id.keyboard_view);
this.mKeyboardView = keyboardView;
this.mKeyboardView.setKeyboard(mKeyboard);
this.mKeyboardView.setEnabled(true);
this.mKeyboardView.setPreviewEnabled(false);
this.mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
frameLayout.addView(mViewContainer, layoutParams);
mViewContainer.setAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.key_enter));
}
/**
* 隐藏软键盘
*/
public void hideSoftKeyboard() {
if (null != mViewContainer && null != mViewContainer.getParent()){
mViewContainer.setAnimation(AnimationUtils.loadAnimation(mActivity, R.anim.key_exit));
((ViewGroup)mViewContainer.getParent()).removeView(mViewContainer);
}
}
/**
* 判断是否显示
* @return true, 显示; false, 不显示
*/
public boolean isShowing(){
if (null == mViewContainer) return false;
return mViewContainer.getVisibility() == View.VISIBLE;
}
KeyboardView.OnKeyboardActionListener mOnKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {
@Override public void onPress(int i) {}
@Override public void onRelease(int i) {}
@Override public void onKey(int primaryCode, int[] keyCodes) {
if (null != mEditText) keyCode(primaryCode, mEditText);
mKeyboardView.postInvalidate();
}
@Override public void onText(CharSequence charSequence) {}
@Override public void swipeLeft() {}
@Override public void swipeRight() {}
@Override public void swipeDown() {}
@Override public void swipeUp() {}
};
/**
* 字符
* @param primaryCode 主要字符
* @param editText 编辑框
*/
private void keyCode(int primaryCode, EditText editText) {
Editable editable = editText.getText();
int start = editText.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_DELETE) { // 回退
if (editText.hasFocus()) {
if (!TextUtils.isEmpty(editable)){
if (start > 0) editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_DONE) { // 下一步
if (editText.getImeOptions() == EditorInfo.IME_ACTION_NEXT) {
int nextFocusForwardId = editText.getNextFocusForwardId();
View viewById = mActivity.findViewById(nextFocusForwardId);
if (viewById != null) {
editText.clearFocus();
viewById.requestFocus();
return;
}
}
editText.clearFocus();
hideSoftKeyboard();
} else if (primaryCode == -1) { // 上一步 , 切换大小写也是这个code , 但是我们的键盘里没有
} else {
if (editText.hasFocus()) editable.insert(start, Character.toString((char) primaryCode));
}
// mKeyboardView.setKeyboard(mKeyboard); 这是切换大小写
}
}
res/layout/unm_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<View
android:layout_width="@dimen/dp_1"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/dp_10"
android:layout_marginBottom="@dimen/dp_10"
android:background="#CCCCCC" />
<ImageView
android:id="@+id/key_pack_up"
android:layout_width="@dimen/dp_150"
android:layout_height="match_parent"
android:padding="@dimen/dp_10"
android:src="@mipmap/key_exit" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CFD3DC"
android:paddingTop="@dimen/dp_9">
<com.guider.health.common.views.num_key.CarKeyboardView
android:id="@+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#CFD3DC"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@drawable/key_num_back"
android:keyPreviewHeight="@dimen/dp_30"
android:keyTextColor="#000"
android:keyTextSize="30sp"
android:visibility="visible" />
</FrameLayout>
</LinearLayout>
res/xml/num_key.xml
<?xml version="1.0" encoding="UTF-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:horizontalGap="0.0px"
android:keyWidth="10%p"
android:keyHeight="8%p"
android:verticalGap="0.0px">
<!-- 1 2 3 0 -->
<Row android:verticalGap="1%p">
<Key
android:codes="49"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyEdgeFlags="left"
android:keyLabel="1" />
<Key
android:codes="50"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyLabel="2" />
<Key
android:codes="51"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyLabel="3" />
<Key
android:codes="48"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyEdgeFlags="right"
android:keyLabel="0" />
<!-- 上一步 -->
<Key
android:codes="-1"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyEdgeFlags="right"
android:keyLabel="上一步" />
</Row>
<!-- 4 5 6 .-->
<Row android:verticalGap="1%p">
<Key
android:codes="52"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyLabel="4" />
<Key
android:codes="53"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyLabel="5" />
<Key
android:codes="54"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyLabel="6" />
<Key
android:codes="46"
android:horizontalGap="1%p"
android:isRepeatable="false"
android:keyWidth="18.1%p"
android:keyLabel="." />
<!-- 下一步 -->
<Key
android:codes="-4"
android:horizontalGap="1%p"
android:isRepeatable="true"
android:keyWidth="18.1%p"
android:keyHeight="17%p"
android:keyIcon="@drawable/key_ok_back"
android:keyLabel="确定" />
</Row>
<!-- 7 8 9 < -->
<Row android:verticalGap="1%p">
<Key
android:codes="55"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyLabel="7" />
<Key
android:codes="56"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyLabel="8" />
<Key
android:codes="57"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyLabel="9" />
<!-- 删除 -->
<Key
android:codes="-5"
android:horizontalGap="1%p"
android:keyWidth="18.1%p"
android:keyIcon="@mipmap/key_dele" />
</Row>
</Keyboard>
关闭动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="100"
android:fromYDelta="0"
android:toYDelta="100%" />
</set>
进入动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="100"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>