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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【android-自定义键盘的设置】

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

效果圖

第一步,在哪個地方放自定義的鍵盤?那個地方放輸入?

我打算在這個最下面放我的鍵盤

因此在這個xml文件中添加鍵盤的組件,為它設置參數,給它id,長,寬,背景,字體顏色,是否聚焦等

<android.inputmethodservice.KeyboardViewandroid:id="@+id/frag_record_rl_key"android:layout_width="match_parent"android:layout_height="wrap_content"android:keyBackground="@color/grey_f3f3f3"android:keyTextColor="@color/black"android:focusable="true"android:focusableInTouchMode="true"android:paddingTop="1dp"android:layout_alignParentBottom="true"android:shadowColor="@color/white"android:shadowRadius="0.0"/>

輸入的地方 EditText為輸入文本的地方

<EditTextandroid:id="@+id/frag_rl_top_money"android:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="number"android:layout_centerVertical="true"android:layout_alignParentRight="true"android:text="0"android:background="@color/white"/>

第二部,設置鍵盤

第一步我們確定了鍵盤的位置,現在詳細設置這個鍵盤,我們在res目錄下創造一個xml的文件夾,在此文件夾中新建一個key.xml的文件,這個文件將詳細設置鍵盤,具體到每一個按鍵。
android:keyHeight,android:keyWidth為鍵盤按鍵的高度,寬度
code表示代碼這個是有規定的,lable為真正代表的內容

<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"><Keyboard xmlns:android="http://schemas.android.com/apk/res/android"android:keyHeight="50dp"android:keyWidth="25%p"android:horizontalGap="1px"android:verticalGap="1px"><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="-5" 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"/><Key android:codes="-4" android:keyHeight="150dp" android:keyLabel="確定"/></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="-3" android:keyLabel="清零"/><Key android:codes="48" android:keyLabel="0"/><Key android:codes="46" android:keyLabel="."/></Row></Keyboard> </Keyboard>

第三步,鍵盤的邏輯代碼編寫

在java文件夾下建立utils文件夾 keyutils

package com.example.notebook.utils;import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.text.Editable; import android.text.InputType; import android.view.View; import android.widget.EditText;import com.example.notebook.R;public class keyutils {private final Keyboard k1; //自定義的鍵盤private KeyboardView keyboardView;private EditText editText;public keyutils(Keyboard k1, KeyboardView keyboardView, EditText editText) {this.k1 = k1;this.keyboardView = keyboardView;this.editText = editText;this.editText.setInputType(InputType.TYPE_NULL);//取消彈出框//放入內容和我們定義好的組件k1 = new Keyboard(this.editText.getContext(), R.xml.key);//設置鍵盤樣式this.keyboardView.setKeyboard(k1);this.keyboardView.setEnabled(true);this.keyboardView.setPreviewEnabled(false);this.keyboardView.setOnKeyboardActionListener(listener);//設置鍵盤按鈕被點擊了的監聽}private interface OnEnsureListener{public void onEnsure();}OnEnsureListener onEnsureListener;public void setOnEnsureListener(OnEnsureListener onEnsureListener) {this.onEnsureListener = onEnsureListener;}KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {@Overridepublic void onPress(int i) {}@Overridepublic void onRelease(int i) {}@Overridepublic void onKey(int i, int[] ints) {Editable editable = editText.getText();int start = editText.getSelectionStart();switch (i) {case Keyboard.KEYCODE_DELETE: //點擊了刪除鍵if (editable!=null &&editable.length()>0) {if (start>0) {editable.delete(start-1,start);}}break;case Keyboard.KEYCODE_CANCEL: //點擊了清零editable.clear();break;case Keyboard.KEYCODE_DONE: //點擊了完成onEnsureListener.onEnsure(); //通過接口回調的方法,當點擊確定時,可以調用這個方法break;default: //其他數字直接插入editable.insert(start,Character.toString((char)i));break;}}@Overridepublic void onText(CharSequence charSequence) {}@Overridepublic void swipeLeft() {}@Overridepublic void swipeRight() {}@Overridepublic void swipeDown() {}@Overridepublic void swipeUp() {}};// 顯示鍵盤public void showKey(){int visibility = keyboardView.getVisibility();if(visibility == View.INVISIBLE|| visibility == View.GONE){keyboardView.setVisibility(View.VISIBLE);}}// 隱藏鍵盤public void hideKeyboard(){int visibility = keyboardView.getVisibility();if (visibility== View.VISIBLE||visibility==View.INVISIBLE) {keyboardView.setVisibility(View.GONE);}} }

總結

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

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