日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义车牌软键盘

發布時間:2023/12/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义车牌软键盘 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前人栽樹后人涼

2015年的一篇文章,公布了核心代碼,源碼下載需要5C幣。
Android 一個簡易的自定義軟鍵盤

2018年的一篇文章,在15年文章的基礎上,公布了絕大多數代碼,源碼托管在Github上。
Android 自定義車牌鍵盤

這文章雖然是自己寫的,但核心思想和代碼還是離不開前輩們的基礎,在此致謝!!
總的說要自定義軟鍵盤,要解決三件事:
1.軟鍵盤元素和布局制作。
2.軟鍵盤彈起、消失和具體操作。
3.系統鍵盤的抑制。

下面來詳細說一下:


一、軟鍵盤的制作需要Keyboard類型的XML文件內部用Row,來詳細規定一共有幾行,每行有什么元素,以及元素的位置和編號。
由于是車牌,所以涉及:1.省簡稱,2.大寫字母A-Z,3.數字0-9。布局的設計是省簡稱一組,數字字母另一組,所以需要兩組布局。
第一組省簡稱:province_abbreviation.xml

<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:horizontalGap="0.0px"android:keyHeight="8%p"android:keyWidth="10%p"android:verticalGap="0.0px"><Row android:verticalGap="1%p"><Key android:codes="20140"android:horizontalGap="1%p"android:keyEdgeFlags="left"android:keyLabel="京"android:keyWidth="8%p" /><Key android:codes="27941"android:horizontalGap="2%p"android:keyLabel="津"android:keyWidth="8%p" /><Key android:codes="20864"android:horizontalGap="2%p"android:keyLabel="冀"android:keyWidth="8%p" /><Key android:codes="40065"android:horizontalGap="2%p"android:keyLabel="魯"android:keyWidth="8%p" /><Key android:codes="26187"android:horizontalGap="2%p"android:keyLabel="晉"android:keyWidth="8%p" /><Key android:codes="33945"android:horizontalGap="2%p"android:keyLabel="蒙"android:keyWidth="8%p" /><Key android:codes="36797"android:horizontalGap="2%p"android:keyLabel="遼"android:keyWidth="8%p" /><Key android:codes="21513"android:horizontalGap="2%p"android:keyLabel="吉"android:keyWidth="8%p" /><Key android:codes="40657"android:horizontalGap="2%p"android:keyLabel="黑"android:keyWidth="8%p" /><Key android:codes="27818"android:horizontalGap="2%p"android:keyEdgeFlags="right"android:keyLabel="滬"android:keyWidth="8%p" /></Row><Row android:verticalGap="1%p"><Key android:codes="33487"android:horizontalGap="1%p"android:keyEdgeFlags="left"android:keyLabel="蘇"android:keyWidth="8%p" /><Key android:codes="27993"android:horizontalGap="2%p"android:keyLabel="浙"android:keyWidth="8%p" /><Key android:codes="30358"android:horizontalGap="2%p"android:keyLabel="皖"android:keyWidth="8%p" /><Key android:codes="38397"android:horizontalGap="2%p"android:keyLabel="閩"android:keyWidth="8%p" /><Key android:codes="36195"android:horizontalGap="2%p"android:keyLabel="贛"android:keyWidth="8%p" /><Key android:codes="35947"android:horizontalGap="2%p"android:keyLabel="豫"android:keyWidth="8%p" /><Key android:codes="37122"android:horizontalGap="2%p"android:keyLabel="鄂"android:keyWidth="8%p" /><Key android:codes="28248"android:horizontalGap="2%p"android:keyLabel="湘"android:keyWidth="8%p" /><Key android:codes="31908"android:horizontalGap="2%p"android:keyLabel="粵"android:keyWidth="8%p" /><Key android:codes="26690"android:horizontalGap="2%p"android:keyEdgeFlags="right"android:keyLabel="桂"android:keyWidth="8%p" /></Row><Row android:verticalGap="1%p"><Key android:codes="28189"android:horizontalGap="11%p"android:keyEdgeFlags="left"android:keyLabel="渝"android:keyWidth="8%p" /><Key android:codes="24029"android:horizontalGap="2%p"android:keyLabel="川"android:keyWidth="8%p" /><Key android:codes="36149"android:horizontalGap="2%p"android:keyLabel="貴"android:keyWidth="8%p" /><Key android:codes="20113"android:horizontalGap="2%p"android:keyLabel="云"android:keyWidth="8%p" /><Key android:codes="34255"android:horizontalGap="2%p"android:keyLabel="藏"android:keyWidth="8%p" /><Key android:codes="38485"android:horizontalGap="2%p"android:keyLabel="陜"android:keyWidth="8%p" /><Key android:codes="29976"android:horizontalGap="2%p"android:keyLabel="甘"android:keyWidth="8%p" /><Key android:codes="38738"android:horizontalGap="2%p"android:keyEdgeFlags="right"android:keyLabel="青"android:keyWidth="8%p" /></Row><Row><Key android:codes="-1"android:horizontalGap="4%p"android:isModifier="true"android:isSticky="true"android:keyEdgeFlags="left"android:keyLabel="ABC"android:keyWidth="15%p" /><Key android:codes="29756"android:horizontalGap="8%p"android:keyLabel="瓊"android:keyWidth="8%p" /><Key android:codes="26032"android:horizontalGap="2%p"android:keyLabel="新"android:keyWidth="8%p" /><Key android:codes="23425"android:horizontalGap="2%p"android:keyLabel="寧"android:keyWidth="8%p" /><Key android:codes="28207"android:horizontalGap="2%p"android:keyLabel="港"android:keyWidth="8%p" /><Key android:codes="28595"android:horizontalGap="2%p"android:keyLabel="澳"android:keyWidth="8%p" /><Key android:codes="21488"android:horizontalGap="2%p"android:keyLabel="臺"android:keyWidth="8%p" /><Key android:codes="-3"android:horizontalGap="8%p"android:isRepeatable="true"android:keyEdgeFlags="right"android:keyLabel="刪除"android:keyWidth="13%p" /></Row></Keyboard>

省簡稱分了四行,這里要注意下最后一行“ABC”。它是切換的關鍵,android:codes=”-1”中的“-1”是鍵位的編號,這個在做觸發操作時會用到,其他的屬性有時間可以研究。

第二組字母數字:number_or_letters.xml

<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:horizontalGap="0.0px"android:keyHeight="8%"android:keyWidth="10%p"android:verticalGap="0.0px"><Row android:verticalGap="1%p"><Keyandroid:codes="49"android:horizontalGap="1%p"android:keyEdgeFlags="left"android:keyLabel="1"android:keyWidth="8%p" /><Keyandroid:codes="50"android:horizontalGap="2%p"android:keyLabel="2"android:keyWidth="8%p" /><Keyandroid:codes="51"android:horizontalGap="2%p"android:keyLabel="3"android:keyWidth="8%p" /><Keyandroid:codes="52"android:horizontalGap="2%p"android:keyLabel="4"android:keyWidth="8%p" /><Keyandroid:codes="53"android:horizontalGap="2%p"android:keyLabel="5"android:keyWidth="8%p" /><Keyandroid:codes="54"android:horizontalGap="2%p"android:keyLabel="6"android:keyWidth="8%p" /><Keyandroid:codes="55"android:horizontalGap="2%p"android:keyLabel="7"android:keyWidth="8%p" /><Keyandroid:codes="56"android:horizontalGap="2%p"android:keyLabel="8"android:keyWidth="8%p" /><Keyandroid:codes="57"android:horizontalGap="2%p"android:keyLabel="9"android:keyWidth="8%p" /><Keyandroid:codes="48"android:horizontalGap="2%p"android:keyEdgeFlags="right"android:keyLabel="0"android:keyWidth="8%p" /></Row><Row android:verticalGap="1%p"><Keyandroid:codes="81"android:horizontalGap="1%p"android:keyEdgeFlags="left"android:keyLabel="Q"android:keyWidth="8%p" /><Keyandroid:codes="87"android:horizontalGap="2%p"android:keyLabel="W"android:keyWidth="8%p" /><Keyandroid:codes="69"android:horizontalGap="2%p"android:keyLabel="E"android:keyWidth="8%p" /><Keyandroid:codes="82"android:horizontalGap="2%p"android:keyLabel="R"android:keyWidth="8%p" /><Keyandroid:codes="84"android:horizontalGap="2%p"android:keyLabel="T"android:keyWidth="8%p" /><Keyandroid:codes="89"android:horizontalGap="2%p"android:keyLabel="Y"android:keyWidth="8%p" /><Keyandroid:codes="85"android:horizontalGap="2%p"android:keyLabel="U"android:keyWidth="8%p" /><Keyandroid:codes="73"android:horizontalGap="2%p"android:keyLabel="I"android:keyWidth="8%p" /><Keyandroid:codes="79"android:horizontalGap="2%p"android:keyLabel="O"android:keyWidth="8%p" /><Keyandroid:codes="80"android:horizontalGap="2%p"android:keyEdgeFlags="right"android:keyLabel="P"android:keyWidth="8%p" /></Row><Row android:verticalGap="1%p"><Keyandroid:codes="65"android:horizontalGap="6%p"android:keyEdgeFlags="left"android:keyLabel="A"android:keyWidth="8%p" /><Keyandroid:codes="83"android:horizontalGap="2%p"android:keyLabel="S"android:keyWidth="8%p" /><Keyandroid:codes="68"android:horizontalGap="2%p"android:keyLabel="D"android:keyWidth="8%p" /><Keyandroid:codes="70"android:horizontalGap="2%p"android:keyLabel="F"android:keyWidth="8%p" /><Keyandroid:codes="71"android:horizontalGap="2%p"android:keyLabel="G"android:keyWidth="8%p" /><Keyandroid:codes="72"android:horizontalGap="2%p"android:keyLabel="H"android:keyWidth="8%p" /><Keyandroid:codes="74"android:horizontalGap="2%p"android:keyLabel="J"android:keyWidth="8%p" /><Keyandroid:codes="75"android:horizontalGap="2%p"android:keyLabel="K"android:keyWidth="8%p" /><Keyandroid:codes="76"android:horizontalGap="2%p"android:keyEdgeFlags="right"android:keyLabel="L"android:keyWidth="8%p" /></Row><Row><Keyandroid:codes="-1"android:horizontalGap="1%p"android:isModifier="true"android:isSticky="true"android:keyEdgeFlags="left"android:keyLabel="省份"android:keyWidth="13%p" /><Keyandroid:codes="90"android:horizontalGap="3%p"android:keyLabel="Z"android:keyWidth="8%p" /><Keyandroid:codes="88"android:horizontalGap="2%p"android:keyLabel="X"android:keyWidth="8%p" /><Keyandroid:codes="67"android:horizontalGap="2%p"android:keyLabel="C"android:keyWidth="8%p" /><Keyandroid:codes="86"android:horizontalGap="2%p"android:keyLabel="V"android:keyWidth="8%p" /><Keyandroid:codes="66"android:horizontalGap="2%p"android:keyLabel="B"android:keyWidth="8%p" /><Keyandroid:codes="78"android:horizontalGap="2%p"android:keyLabel="N"android:keyWidth="8%p" /><Keyandroid:codes="77"android:horizontalGap="2%p"android:keyLabel="M"android:keyWidth="8%p" /><Keyandroid:codes="-3"android:horizontalGap="3%p"android:isRepeatable="true"android:keyEdgeFlags="right"android:keyLabel="刪除"android:keyWidth="13%p" /></Row> </Keyboard>

同上,這個切換在省份,code也是-1。
然后是放置軟鍵盤,由于是自定義的,所以需要放置在相應的布局中(include應該也可以,我沒試過)。要在底端顯示,布局最好是RelativeLayout。示例代碼如下

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditText android:id="@+id/et_keyboard"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_margin="20dp"android:background="#ACE"android:hint="車牌號"android:padding="10dp" /><android.inputmethodservice.KeyboardView android:id="@+id/keyboard_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:background="#DCDCDC"android:focusable="true"android:focusableInTouchMode="true"android:keyBackground="@drawable/selector_key"android:keyTextColor="#000"android:keyTextSize="16sp"android:paddingBottom="8dp"android:paddingTop="8dp"android:shadowColor="#FFFFFF"android:shadowRadius="0.0"android:visibility="gone" /></RelativeLayout>

相信我,自定義軟鍵盤如果難看,會非常難看!!我們需要加上樣式,
即:android:keyBackground=”@drawable/selector_key”
selector_key.xml:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/shape_key_normal" android:state_pressed="true" /><item android:drawable="@drawable/shape_key_pressed" /> </selector>

shape_key_normal.xml:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="5dp"/><solid android:color="@android:color/darker_gray" /> </shape>

shape_key_pressed.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="5dp"/><solid android:color="#ffffff" /> </shape>

布局到這里就完了,具體情況需要微調布局。下面是軟鍵盤的處理邏輯。


二、軟鍵盤的處理邏輯
由于需要對軟鍵盤進行一系列的操作,所以最好定義一個工具類來處理這些事,這也是比較關鍵的一環。
所以定義KeyboardUtil類,由于習慣的原因和18年那篇文章有些不同鍵盤可以切換,允許出現不符合格式的情況,示例代碼如下:

package com.xxx.xxx.utils;import android.app.Activity; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.text.Editable; import android.text.InputType; import android.view.View; import android.view.WindowManager; import android.widget.EditText; import com.haorizi.dipuchuangxin.R; import com.orhanobut.logger.Logger; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;/*** Class Name:* Created by xxx on 2018/7/11.* 備注:自定義軟鍵盤的工具類** @version 2018071101*/ public class KeyboardUtil {private Activity mActivity;private KeyboardView mKeyboardView;private EditText mEdit;/*** 省簡稱鍵盤*/private Keyboard provinceKeyboard;/*** 數字、字母鍵盤*/private Keyboard numberKeyboard;private boolean isProvince = true;public KeyboardUtil(Activity activity, EditText edit) {mActivity = activity;mEdit = edit;// 綁定布局numberKeyboard = new Keyboard(activity, R.xml.number_or_letters);provinceKeyboard = new Keyboard(activity, R.xml.province_abbreviation);// 還是別用ButterKnife了mKeyboardView = (KeyboardView) activity.findViewById(R.id.keyboard_view);// 設置省簡稱鍵盤mKeyboardView.setKeyboard(provinceKeyboard);// 設置焦點mKeyboardView.setEnabled(true);mKeyboardView.setPreviewEnabled(false);// 鍵盤監聽事件mKeyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {@Overridepublic void onPress(int primaryCode) {}@Overridepublic void onRelease(int primaryCode) {}@Overridepublic void onKey(int primaryCode, int[] keyCodes) {Editable editable = mEdit.getText();int start = mEdit.getSelectionStart();// 判定是否是中文的正則表達式 [\\u4e00-\\u9fa5]判斷一個中文 [\\u4e00-\\u9fa5]+多個中文String reg = "[\\u4e00-\\u9fa5]";Logger.e("= ==" + primaryCode);if (primaryCode == -1) {// 省份簡稱與數字鍵盤切換 // if (mEdit.getText().toString().matches(reg)) {Logger.e("切換。。。");if (isProvince) {// 切數字changeKeyboard(true);} else {// 切省份changeKeyboard(false);} // }} else if (primaryCode == -3) {if (editable != null && editable.length() > 0) {//沒有輸入內容時軟鍵盤重置為省份簡稱軟鍵盤if (editable.length() == 1) {changeKeyboard(false);}if (start > 0) {editable.delete(start - 1, start);}}} else {editable.insert(start, Character.toString((char) primaryCode));// 判斷第一個字符是否是中文,是,則自動切換到數字軟鍵盤if (mEdit.getText().toString().matches(reg)) {changeKeyboard(true);}}}@Overridepublic void onText(CharSequence text) {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeDown() {}@Overridepublic void swipeUp() {}});}/*** 指定切換軟鍵盤 isNumber false表示要切換為省份簡稱軟鍵盤 true表示要切換為數字軟鍵盤*/private void changeKeyboard(boolean isNumber) {if (isNumber) {// 切數字mKeyboardView.setKeyboard(numberKeyboard);isProvince = false;} else {// 切省份mKeyboardView.setKeyboard(provinceKeyboard);isProvince = true;}}/*** 軟鍵盤展示狀態*/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<EditText> 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 e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}} }

這部分的代碼大同小異。然后是Activity中的引用(只在需要車牌的地方觸發,其他的Edit Text是不影響的)。


三、Activity示例代碼:

package com.xxx.xxx;import android.app.Activity; import android.content.Context; import android.content.Intent; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.TextView; import com.haorizi.dipuchuangxin.ui.BaseAty; import com.haorizi.dipuchuangxin.utils.KeyboardUtil; import com.haorizi.dipuchuangxin.utils.PtrHelper; import com.orhanobut.logger.Logger; import butterknife.Bind; import butterknife.OnClick; import in.srain.cube.views.ptr.PtrDefaultHandler; import in.srain.cube.views.ptr.PtrFrameLayout;/*** Class Name:* Created by xxx .* 簡介:* Data: 2018/5/14.** @version 2018061401 xxx*/public class TextAty extends BaseAty {@Bind(R.id.keyboard_view)KeyboardView mKeyboardView;@Bind(R.id.et_keyboard)EditText et;private KeyboardUtil keyboardUtil;@Overridepublic int getLayoutId() {return R.layout.soft_key_board;}@Overridepublic void initData() {// 觸發自定義鍵盤et.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (keyboardUtil == null){keyboardUtil = new KeyboardUtil(TextAty.this, et);keyboardUtil.hideSoftInputMethod();keyboardUtil.showKeyboard();} else {keyboardUtil.showKeyboard();}return false;}});et.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {}});}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK){//if (keyboardUtil.isShow()){// keyboardUtil.hideKeyboard();//} else {// finish();// }// version 2018071801if(keyboardUtil != null && keyboardUtil.isShow()){keyboardUtil.hideKeyboard();} else {finish();}}return false;}@Overridepublic void requestData() {Logger.e("stop refresh...");showToast("stop refresh...");} }

至此完結,眼下鍵盤還有不能自己消失,需要點擊手機上的返回鍵,后期優化吧,這倒不難。


version 2018171801 剛發現一個錯誤,在返回鍵監聽的方法中,要先判斷keyboardUtil是否為null,如果沒彈起直接點返回會報空指針。

總結

以上是生活随笔為你收集整理的自定义车牌软键盘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。