ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

安卓自定义软键盘

2022-02-05 19:02:19  阅读:195  来源: 互联网

标签:自定义 int editText 安卓 keyboardView public 软键盘 Override void


在res下建立xml包,在xml包内建立keyboard.xml文件:

 1 <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:keyHeight="50dp"
 3     android:keyWidth="%25p"
 4     android:horizontalGap="1dp"
 5     android:verticalGap="1dp">
 6     <!-- keyHeight每一个按键的高度,keyWidth每一个按键的宽度,horizontalGap水平分割线   -->
 7     <Row>
 8         <Key android:codes="49" android:keyLabel="1"/>
 9         <Key android:codes="50" android:keyLabel="2"/>
10         <Key android:codes="51" android:keyLabel="3"/>
11         <Key android:codes="-5" android:keyLabel="删除"/>
12     </Row>
13     <!-- codes是ascii码,然后删除键是特定的-5 -->
14     <Row>
15         <Key android:codes="52" android:keyLabel="4"/>
16         <Key android:codes="53" android:keyLabel="5"/>
17         <Key android:codes="54" android:keyLabel="6"/>
18         <Key android:codes="-4" android:keyHeight="150dp" android:keyLabel="确定"/>
19     </Row>
20     <Row>
21         <Key android:codes="55" android:keyLabel="7"/>
22         <Key android:codes="56" android:keyLabel="8"/>
23         <Key android:codes="57" android:keyLabel="9"/>
24     </Row>
25     <Row>
26         <Key android:codes="-3" android:keyLabel="清零"/>
27         <Key android:codes="48" android:keyLabel="0"/>
28         <Key android:codes="46" android:keyLabel="."/>
29     </Row>
30 </Keyboard>

然后又建立了一个utils包,在该包下建立keyBoardUtils.java文件:

 1 public class keyBoardUtils {
 2     private final Keyboard k1;
 3     private KeyboardView keyboardView;
 4     private EditText editText;
 5 
 6     public interface OnEnsureListener{
 7         public void OnEnsure();
 8     }
 9     OnEnsureListener onEnsureListener;
10     public void setOnEnsureListener(OnEnsureListener onEnsureListener){
11         this.onEnsureListener=onEnsureListener;
12     }
13 
14     public keyBoardUtils(KeyboardView keyboardView, EditText editText) {
15         this.keyboardView = keyboardView;
16         this.editText = editText;
17         this.editText.setInputType(InputType.TYPE_NULL);//取消弹出系统键盘
18         k1=new Keyboard(this.editText.getContext(), R.xml.keyboard);
19         this.keyboardView.setKeyboard(k1);
20         this.keyboardView.setEnabled(true);
21         this.keyboardView.setPreviewEnabled(false);//禁止弹出
22         this.keyboardView.setOnKeyboardActionListener(listener);//设置键盘按钮点击监听
23     }
24     KeyboardView.OnKeyboardActionListener listener=new KeyboardView.OnKeyboardActionListener() {
25         @Override
26         public void onPress(int i) {
27 
28         }
29 
30         @Override
31         public void onRelease(int i) {
32 
33         }
34         @Override
35         public void onKey(int i, int[] ints) {
36             Editable editable = editText.getText();
37             int start=editText.getSelectionStart();
38             switch (i){
39                 case Keyboard.KEYCODE_DELETE://点击了删除
40                     if(editable!=null&&editable.length()>0){
41                         if(start>0){
42                             editable.delete(start-1,start);
43                         }
44                     }
45                     break;
46                 case Keyboard.KEYCODE_CANCEL://点击了清零
47                     editable.clear();
48                     break;
49                 case Keyboard.KEYCODE_DONE://点击了确定
50                     onEnsureListener.OnEnsure();//通过接口回调的方法,点击确定调用该方法
51                     break;
52                 default://其他的数字直接插入
53                     editable.insert(start,Character.toString((char)i));
54                     break;
55             }
56         }
57         @Override
58         public void onText(CharSequence charSequence) { }
59         @Override
60         public void swipeLeft() { }
61         @Override
62         public void swipeRight() { }
63         @Override
64         public void swipeDown() { }
65         @Override
66         public void swipeUp() { }
67     };
68 
69     public void showKewboard(){
70         int visibility=keyboardView.getVisibility();
71         if(visibility==View.INVISIBLE||visibility==View.GONE){
72             keyboardView.setVisibility(View.VISIBLE);
73         }
74     }//显示键盘
75 
76     public void hideKeyboard(){
77         int visibility=keyboardView.getVisibility();
78         if(visibility==View.VISIBLE||visibility==View.INVISIBLE){
79             keyboardView.setVisibility(View.GONE);
80         }
81     }//隐藏键盘
82 }

 

标签:自定义,int,editText,安卓,keyboardView,public,软键盘,Override,void
来源: https://www.cnblogs.com/konglong-cm/p/15864600.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有