这篇文章写的自定义键盘有个bug,两个输入框同时在的时候,自定义的edittext光标不显示,特在此记录解决办法修改ontouchevent 中的代码为:
override fun onTouchEvent(event: MotionEvent?): Boolean { hideSystemKeyBoard() isCursorVisible=true if (event?.action == MotionEvent.ACTION_UP) { if (keyboardView?.visibility != View.VISIBLE) { keyboardView?.visibility = View.VISIBLE viewGroup?.visibility = View.VISIBLE listener?.show() } } return super.onTouchEvent(event) }其中,hideSystemKeyBoard(),代码如下
private fun hideSystemKeyBoard() { val imm = this.context_?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager ?: return val isOpen = imm.isActive if (isOpen) { imm.hideSoftInputFromWindow(windowToken, 0) } val currentVersion = Build.VERSION.SDK_INT var methodName: String? = null if (currentVersion >= 16) { methodName = "setShowSoftInputOnFocus" } else if (currentVersion >= 14) { methodName = "setSoftInputShownOnFocus" } if (methodName == null) { inputType = 0 } else { try { val setShowSoftInputOnFocus = EditText::class.java.getMethod(methodName, java.lang.Boolean.TYPE) setShowSoftInputOnFocus.isAccessible = true setShowSoftInputOnFocus.invoke(this, java.lang.Boolean.FALSE) } catch (e: NoSuchMethodException) { inputType = 0 e.printStackTrace() } catch (e: IllegalAccessException) { e.printStackTrace() } catch (e: InvocationTargetException) { e.printStackTrace() } catch (e: IllegalArgumentException) { e.printStackTrace() } } }在activity中,控制自定义键盘的显示
login_input_mobile_et.setOnTouchListener { v, event -> login_password_psd_et.hideKeyBoard() super.onTouchEvent(event) }问题解决
转载于:https://www.cnblogs.com/zhdsky/p/11078265.html
相关资源:JAVA上百实例源码以及开源项目