素材巴巴 > 程序开发 >

Android自定义实现漂亮的软键盘

程序开发 2023-09-26 07:12:20

工作中遇到了自定义软键盘的场景,虽然简单很快就实现了,但对个别的细节不太满意。

因为细节决定成败,所以细节之处也不能忽视。

先来张效果图吧:

我对这个自定义软键盘做了个简单的封装,使用起来也很简单。以下是我的自定义软键盘View类:

package com.newcapec.visitorsystem.diyview;import android.app.Activity;
 import android.inputmethodservice.Keyboard;
 import android.inputmethodservice.KeyboardView;
 import android.text.Editable;
 import android.text.InputType;
 import android.util.Log;
 import android.view.View;
 import android.view.WindowManager;
 import android.widget.EditText;import com.newcapec.cardliarbry.VistorCardController;
 import com.newcapec.visitorsystem.R;
 import com.newcapec.visitorsystem.interf.OnFinishListener;import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.List;/*** @author : xieqinzhong* @date :2020/6/16 14:52* @description:**/
 public class AbcNumberView extends View {private Activity mActivity;private MyKeyboardView mKeyboardView;private EditText mEdit;/*** 数字与大写字母键盘*/private Keyboard numberKeyboard;/** 确认回调*/private OnFinishListener finishListener;private KeyboardView keyboardView;/**  id: 布局id**/public AbcNumberView(int viewId,Activity activity,boolean includeNumber, EditText edit,OnFinishListener finishListener) {super(activity);mActivity = activity;mEdit = edit;this.finishListener = finishListener;//R.xml.abc_and_numberif (includeNumber) {numberKeyboard = new Keyboard(activity, R.xml.abc_and_number);}else {numberKeyboard = new Keyboard(activity, R.xml.abc_key);}mKeyboardView = (MyKeyboardView) activity.findViewById(viewId);mKeyboardView.setKeyboard(numberKeyboard);mKeyboardView.setEnabled(true);mKeyboardView.setPreviewEnabled(false);mKeyboardView.setOnKeyboardActionListener(listener);mKeyboardView.bringToFront();}private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {@Overridepublic void swipeUp() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeDown() {}@Overridepublic void onText(CharSequence text) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onPress(int primaryCode) {}@Overridepublic void onKey(int primaryCode, int[] keyCodes) {Editable editable = mEdit.getText();int start = mEdit.getSelectionStart();//判定是否是中文的正则表达式 [\u4e00-\u9fa5]判断一个中文 [\u4e00-\u9fa5]+多个中文if (primaryCode == -1) {// 确定键hideKeyboard();beginSearch(finishListener,mEdit.getText().toString());} else if (primaryCode == -3) {//删除键if (editable != null && editable.length() > 0) {if (start > 0) {editable.delete(start - 1, start);}}}else {editable.insert(start, Character.toString((char) primaryCode));}}};private void beginSearch(final OnFinishListener onFinishListener, String value) {finishListener.search(value);}/*** 软键盘展示状态*/public boolean isShow() {return mKeyboardView.getVisibility() == View.VISIBLE;}/*** 软键盘展示*/public void showKeyboard() {int visibility = mKeyboardView.getVisibility();if (visibility == View.GONE || visibility == View.INVISIBLE) {mKeyboardView.setVisibility(View.VISIBLE);}}/*** 软键盘隐藏*/public void  hideKeyboard() {int visibility = mKeyboardView.getVisibility();if (visibility == View.VISIBLE) {mKeyboardView.setVisibility(View.INVISIBLE);}}/*** 禁掉系统软键盘*/public void hideSoftInputMethod() {mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);int currentVersion = android.os.Build.VERSION.SDK_INT;String methodName = null;if (currentVersion >= 16) {// 4.2methodName = "setShowSoftInputOnFocus";} else if (currentVersion >= 14) {// 4.0methodName = "setSoftInputShownOnFocus";}if (methodName == null) {mEdit.setInputType(InputType.TYPE_NULL);} else {Class cls = EditText.class;Method setShowSoftInputOnFocus;try {setShowSoftInputOnFocus = cls.getMethod(methodName, boolean.class);setShowSoftInputOnFocus.setAccessible(true);setShowSoftInputOnFocus.invoke(mEdit, false);} catch (NoSuchMethodException e) {mEdit.setInputType(InputType.TYPE_NULL);e.printStackTrace();} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {e.printStackTrace();}}}}
 

 布局文件如下:


 

使用也很简单:

 mainBinding.includeBackscreenSearchLayout.edtInputPhone.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_UP) {abcNumberViewb.setOkListener(edtInputPhoneOkListener);abcNumberViewb.setmEdit( mainBinding.includeBackscreenSearchLayout.edtInputPhone);if(!abcNumberViewb.isShow()){abcNumberViewb.showKeyboard();}}return false;}});


标签:

上一篇: 9 个值得推荐的 VUE3 UI 框架 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。