├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── markdown-navigator.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zyl │ │ └── customkeyboard │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zyl │ │ │ └── customkeyboard │ │ │ ├── KeyboardEditTextActivity.java │ │ │ ├── MainActivity.java │ │ │ └── SimpleUsageActivity.java │ └── res │ │ ├── layout │ │ ├── activity_keyboard_edittext.xml │ │ ├── activity_main.xml │ │ └── activity_simpleusage.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── zyl │ └── customkeyboard │ └── ExampleUnitTest.java ├── build.gradle ├── customkeyboard ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zyl │ │ └── customkeyboardview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zyl │ │ │ └── customkeyboardview │ │ │ └── CustomKeyboardEditText.java │ └── res │ │ ├── drawable │ │ └── bg_keyboard_btn.xml │ │ ├── layout │ │ └── keyboard_view.xml │ │ ├── mipmap-xxxhdpi │ │ └── keyboard_del.png │ │ ├── values │ │ ├── attr.xml │ │ └── strings.xml │ │ └── xml │ │ └── idcard_keyboard.xml │ └── test │ └── java │ └── com │ └── zyl │ └── customkeyboardview │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 35 | 36 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CustomKeyboardView 2 | android自定义键盘、自定义身份证键盘、支持拓展。 3 | android 系统键盘支持的点已经比较丰富了, 但是有时候某一些需求还不能满足我们的需求。最近公司应用到了实名认证相关的功能,这部分需要一个身份证的EditText, 自然也需要一个身份证的键盘,奈何系统没有这种键盘,只能自定义一个。 4 | 5 | 首先来看android SDK为我们提供**Keyboard**的这个类。 6 | 7 | 1、Keyboard xml描述文件 8 | 9 | ![来源android开发者官网](http://upload-images.jianshu.io/upload_images/2229793-a623286be02606ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 10 | 11 | 上面已经描述的很清晰了, 它用来加载一个键盘和存储按键属性的一个描述XML。我们在res/xml 目录下新建一个idcard_keyboard.xml文件。 12 | 13 | ``` 14 | 15 | 20 | 21 | 24 | 27 | 30 | 31 | 32 | 35 | 38 | 41 | 42 | 43 | 46 | 49 | 52 | 53 | 54 | 58 | 61 | 65 | 66 | 67 | ``` 68 | 这里主要介绍一些常用的类属性,更多请参考[andorid官网](https://developer.android.google.cn/reference/android/inputmethodservice/Keyboard.html)。 69 | - Keyboard类:存储键盘以及按键相关信息。 70 | android:horizontalGap 按键之间默认的水平间距。 71 | android:verticalGap 按键之间默认的垂直间距。 72 | android:keyHeight 按键的默认高度,以像素或显示高度的百分比表示。 73 | android:keyWidth: 按键的默认宽度,以像素或显示宽度的百分比表示。 74 | - Row:为包含按键的容器。 75 | - Key: 用于描述键盘中单个键的位置和特性。 76 | android:codes 该键输出的unicode值。 77 | android:isRepeatable 这个属性如果设置为true,那么当长按该键时就会重复接受到该键上的动作,在 删除键键 和 空格键 上通常设为true。 78 | android:keyLabel 显示在按键上的文字。 79 | android:keyIcon 与keyLabel是二选一关系,它会代替文字以图标的形式显示在键上。 80 | 81 | 有了Keyboard来存储键盘相关信息了,那么键盘如何这些信息呢?这时候用到android SDK为我们提供的另外一个类***KeyboardView***。 82 | 83 | 2、KeyboardView 84 | 85 | ![来自android开发者官网.png](http://upload-images.jianshu.io/upload_images/2229793-8bf9a16ad3887345.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 86 | 87 | KeyboardView是一个渲染虚拟键盘的View。 它处理键的渲染和检测按键和触摸动作。显然我们需要KeyboardView来对Keyboard里的数据进行渲染并呈现给我们以及相关的点击事件做处理。 88 | 1)`//设置keyboard与KeyboardView相关联的方法。 89 | public void setKeyboard(Keyboard keyboard) ` 90 | 2)`//设置虚拟键盘事件的监听,此方法必须设置,不然会报错。 91 | public void setOnKeyboardActionListener(OnKeyboardActionListener listener)` 92 | 步骤上呢,做完第一步的关联,并设置第二步的事件,调用`KeyboardView.setVisible(true);`键盘就可以显示出来了, 是不是很简单。不过到这里还没有结束哦,接下来我们为了使用上的便利要进行相应的封装。 93 | 封装 94 | 这里我们通过继承EditText来对Keyboard与KeyboardView进行封装。 95 | attr.xml文件,这里我们需要通过一个xml类型的自定义属性引入我们的键盘描述文件。 96 | ``` 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | ``` 105 | 还需要一个keyboardView 布局文件 106 | ``` 107 | 108 | 109 | 121 | ``` 122 | 万事俱备、主角登场~~~ 123 | ``` 124 | /** 125 | * Description: 自定义键盘类KeyboardEditText 126 | * Created by zouyulong on 2017/7/26. 127 | */ 128 | 129 | public class CustomKeyboardEditText extends EditText implements KeyboardView.OnKeyboardActionListener, 130 | View.OnClickListener { 131 | private Keyboard mKeyboard; 132 | private KeyboardView mKeyboardView; 133 | private PopupWindow mKeyboardWindow; 134 | private View mDecorView; 135 | 136 | public CustomKeyboardEditText(Context context) { 137 | this(context, null); 138 | } 139 | 140 | public CustomKeyboardEditText(Context context, AttributeSet attrs) { 141 | this(context, attrs, 0); 142 | } 143 | 144 | public CustomKeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr) { 145 | super(context, attrs, defStyleAttr); 146 | initKeyboardView(context, attrs); 147 | } 148 | 149 | private void initKeyboardView(Context context, AttributeSet attrs) { 150 | TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Keyboard); 151 | if (!array.hasValue(R.styleable.Keyboard_xml)) { 152 | throw new IllegalArgumentException("you need add keyboard_xml argument!"); 153 | } 154 | int xmlId = array.getResourceId(R.styleable.Keyboard_xml, 0); 155 | mKeyboard = new Keyboard(context, xmlId); 156 | mKeyboardView = (KeyboardView) LayoutInflater.from(context).inflate(R.layout.keyboard_view, null); 157 | //键盘关联keyboard对象 158 | mKeyboardView.setKeyboard(mKeyboard); 159 | //关闭键盘按键预览效果,如果按键过小可能会比较适用。 160 | mKeyboardView.setPreviewEnabled(false); 161 | //设置键盘事件 162 | mKeyboardView.setOnKeyboardActionListener(this); 163 | //将keyboardview放入popupwindow方便显示以及位置调整。 164 | mKeyboardWindow = new PopupWindow(mKeyboardView, 165 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 166 | array.recycle(); 167 | //设置点击事件,点击后键盘弹起,系统键盘收起。 168 | setOnClickListener(this); 169 | //屏蔽当前edittext的系统键盘 170 | notSystemSoftInput(); 171 | } 172 | 173 | @Override 174 | public void onPress(int primaryCode) { 175 | 176 | } 177 | 178 | @Override 179 | public void onRelease(int primaryCode) { 180 | 181 | } 182 | 183 | @Override 184 | public void onKey(int primaryCode, int[] keyCodes) { 185 | Editable editable = this.getText(); 186 | //获取光标偏移量下标 187 | int startIndex = this.getSelectionStart(); 188 | switch (primaryCode) { 189 | case Keyboard.KEYCODE_CANCEL:// 隐藏键盘 190 | hideKeyboard(); 191 | break; 192 | case Keyboard.KEYCODE_DELETE:// 回退 193 | if (editable != null && editable.length() > 0) { 194 | if (startIndex > 0) { 195 | editable.delete(startIndex - 1, startIndex); 196 | } 197 | } 198 | break; 199 | case 9994://左移 200 | setSelection(startIndex-1); 201 | break; 202 | case 9995://重输 203 | editable.clear(); 204 | break; 205 | case 9996://右移 206 | if (startIndex < length()) { 207 | setSelection(startIndex + 1); 208 | } 209 | break; 210 | default: 211 | editable.insert(startIndex, Character.toString((char) primaryCode)); 212 | break; 213 | } 214 | } 215 | 216 | @Override 217 | public void onText(CharSequence text) { 218 | 219 | } 220 | 221 | @Override 222 | public void swipeLeft() { 223 | 224 | } 225 | 226 | @Override 227 | public void swipeRight() { 228 | 229 | } 230 | 231 | @Override 232 | public void swipeDown() { 233 | 234 | } 235 | 236 | @Override 237 | public void swipeUp() { 238 | 239 | } 240 | 241 | /** 242 | * 根据key code 获取 Keyboard.Key 对象 243 | * @param primaryCode 244 | * @return 245 | */ 246 | private Keyboard.Key getKeyByKeyCode(int primaryCode) { 247 | if(null != mKeyboard){ 248 | List keyList = mKeyboard.getKeys(); 249 | for (int i =0,size= keyList.size(); i < size; i++) { 250 | Keyboard.Key key = keyList.get(i); 251 | 252 | int codes[] = key.codes; 253 | 254 | if(codes[0] == primaryCode){ 255 | return key; 256 | } 257 | } 258 | } 259 | 260 | return null; 261 | } 262 | 263 | @Override 264 | public boolean onKeyDown(int keyCode, KeyEvent event) { 265 | if (keyCode == KeyEvent.KEYCODE_BACK) { 266 | if (null != mKeyboardWindow) { 267 | if (mKeyboardWindow.isShowing()) { 268 | mKeyboardWindow.dismiss(); 269 | return true; 270 | } 271 | } 272 | } 273 | return super.onKeyDown(keyCode, event); 274 | } 275 | 276 | @SuppressLint("MissingSuperCall") 277 | @Override 278 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 279 | if (!focused) { 280 | hideKeyboard(); 281 | } else { 282 | hideSysInput(); 283 | showKeyboard(); 284 | } 285 | super.onFocusChanged(focused, direction, previouslyFocusedRect); 286 | } 287 | 288 | @Override 289 | public void onAttachedToWindow() { 290 | super.onAttachedToWindow(); 291 | this.mDecorView = ((Activity) getContext()).getWindow().getDecorView(); 292 | hideSysInput(); 293 | } 294 | 295 | @Override 296 | public void onDetachedFromWindow() { 297 | super.onDetachedFromWindow(); 298 | hideKeyboard(); 299 | mKeyboardWindow = null; 300 | mKeyboardView = null; 301 | mKeyboard = null; 302 | mDecorView = null; 303 | } 304 | 305 | /** 306 | * 显示自定义键盘 307 | */ 308 | private void showKeyboard() { 309 | if (null != mKeyboardWindow) { 310 | if (!mKeyboardWindow.isShowing()) { 311 | mKeyboardView.setKeyboard(mKeyboard); 312 | mKeyboardWindow.showAtLocation(this.mDecorView, Gravity.BOTTOM, 0, 0); 313 | } 314 | } 315 | } 316 | 317 | /** 318 | * 屏蔽系统输入法 319 | */ 320 | private void notSystemSoftInput(){ 321 | if (Build.VERSION.SDK_INT <= 10) { 322 | setInputType(InputType.TYPE_NULL); 323 | } else { 324 | ((Activity)getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 325 | try { 326 | Class cls = EditText.class; 327 | Method setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class); 328 | setShowSoftInputOnFocus.setAccessible(true); 329 | setShowSoftInputOnFocus.invoke(this, false); 330 | } catch (Exception e) { 331 | e.printStackTrace(); 332 | } 333 | } 334 | } 335 | 336 | /** 337 | * 隐藏自定义键盘 338 | */ 339 | private void hideKeyboard() { 340 | if (null != mKeyboardWindow) { 341 | if (mKeyboardWindow.isShowing()) { 342 | mKeyboardWindow.dismiss(); 343 | } 344 | } 345 | } 346 | 347 | /** 348 | * 隐藏系统键盘 349 | */ 350 | private void hideSysInput() { 351 | if (this.getWindowToken() != null) { 352 | InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 353 | imm.hideSoftInputFromWindow(this.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 354 | } 355 | } 356 | 357 | @Override 358 | public void onClick(View v) { 359 | requestFocus(); 360 | requestFocusFromTouch(); 361 | hideSysInput(); 362 | showKeyboard(); 363 | } 364 | } 365 | ``` 366 | 代码中注释写的已经很清晰了,就不做一一的讲解了。 367 | 368 | 结尾 369 | - 该自定义组件目前只是支持了身份证键盘,xml下只提供了身份证键盘的描述文件。如果需要其他键盘可以自己定义xml文件,如果有特殊点击事件,逻辑放入`public void onKey(int primaryCode, int[] keyCodes) ` case 相应的keycode逻辑下即可。 370 | 371 | - 代码已放入[github https://github.com/zyl409214686/CustomKeyboardView](https://github.com/zyl409214686/CustomKeyboardView),欢迎提 issues、star、Fork。 372 | 373 | - 效果图: 374 | 375 | ![截图](http://upload-images.jianshu.io/upload_images/2229793-ae3ccc43a3d4cd7c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 376 | 377 | 378 | 参考: 379 | - [android开发者官网](https://developer.android.google.cn/reference/android/inputmethodservice/Keyboard.html) 380 | - [Android 总结:自定义键盘实现原理和三种实例详解](http://blog.csdn.net/u014136472/article/details/50257245) -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.zyl.customkeyboard" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.3.1' 28 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 29 | testCompile 'junit:junit:4.12' 30 | compile project(':customkeyboard') 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Developer/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zyl/customkeyboard/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zyl.customkeyboard; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.zyl.customkeyboard", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyl/customkeyboard/KeyboardEditTextActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyl.customkeyboard; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | 6 | /** 7 | * Description: 封装后的自定义KeyboardEditText 8 | * Created by zouyulong on 2017/7/26. 9 | * Email : zouyulong@syswin.com 10 | * Person in charge : zouyulong 11 | */ 12 | public class KeyboardEditTextActivity extends AppCompatActivity { 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_keyboard_edittext); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyl/customkeyboard/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyl.customkeyboard; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | /** 9 | * Description: 主activity 10 | * Created by zouyulong on 2017/7/26. 11 | * Email : zouyulong@syswin.com 12 | * Person in charge : zouyulong 13 | */ 14 | public class MainActivity extends AppCompatActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | findViewById(R.id.bt_unwrapper).setOnClickListener(new View.OnClickListener() { 21 | @Override 22 | public void onClick(View v) { 23 | Intent in = new Intent(); 24 | in.setClass(MainActivity.this, SimpleUsageActivity.class); 25 | startActivity(in); 26 | } 27 | }); 28 | findViewById(R.id.bt_wrappered).setOnClickListener(new View.OnClickListener() { 29 | @Override 30 | public void onClick(View v) { 31 | Intent in = new Intent(); 32 | in.setClass(MainActivity.this, KeyboardEditTextActivity.class); 33 | startActivity(in); 34 | } 35 | }); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyl/customkeyboard/SimpleUsageActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyl.customkeyboard; 2 | 3 | import android.inputmethodservice.Keyboard; 4 | import android.inputmethodservice.KeyboardView; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | 9 | /** 10 | * Description: 封装前,简单用法 11 | * Created by zouyulong on 2017/7/26. 12 | * Email : zouyulong@syswin.com 13 | * Person in charge : zouyulong 14 | */ 15 | public class SimpleUsageActivity extends AppCompatActivity { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_simpleusage); 21 | final KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboard_idcard); 22 | Keyboard keyboard = new Keyboard(this, R.xml.idcard_keyboard); 23 | keyboardView.setKeyboard(keyboard); 24 | keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() { 25 | @Override 26 | public void onPress(int primaryCode) { 27 | 28 | } 29 | 30 | @Override 31 | public void onRelease(int primaryCode) { 32 | 33 | } 34 | 35 | @Override 36 | public void onKey(int primaryCode, int[] keyCodes) { 37 | 38 | } 39 | 40 | @Override 41 | public void onText(CharSequence text) { 42 | 43 | } 44 | 45 | @Override 46 | public void swipeLeft() { 47 | 48 | } 49 | 50 | @Override 51 | public void swipeRight() { 52 | 53 | } 54 | 55 | @Override 56 | public void swipeDown() { 57 | 58 | } 59 | 60 | @Override 61 | public void swipeUp() { 62 | 63 | } 64 | }); 65 | findViewById(R.id.et_identity_card).setOnClickListener(new View.OnClickListener() { 66 | @Override 67 | public void onClick(View v) { 68 | keyboardView.setVisibility(View.VISIBLE); 69 | } 70 | }); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_keyboard_edittext.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 12 | 13 | 22 | 23 | 28 | 29 | 33 | 34 | 38 | 39 | 47 | 48 | 60 | 61 | 62 | 68 | 69 | 73 | 74 | 82 | 83 | 97 | 98 | 99 | 104 | 105 | 106 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 12 | 13 |