我正在开发一个应用程序并且我已经显示了自定义键盘,我需要禁用 android 默认键盘的出现,并且当我们单击该编辑文本时应该显示光标。
我遵循以下链接(选定的答案),但它们不适用于 android 8.0
该代码用于隐藏键盘出现但没有显示光标。
任何人都可以知道解决方案。
我正在开发一个应用程序并且我已经显示了自定义键盘,我需要禁用 android 默认键盘的出现,并且当我们单击该编辑文本时应该显示光标。
我遵循以下链接(选定的答案),但它们不适用于 android 8.0
该代码用于隐藏键盘出现但没有显示光标。
任何人都可以知道解决方案。
我得到了上述问题的解决方案。
我已经实现了我的自定义键盘(带有数字的视图)以仅在 EditText 中输入数字。
我为屏幕中使用的每个编辑文本都使用了以下功能集。
fun disableSoftInputFromAppearing(editText: CustomEditText) {
if (Build.VERSION.SDK_INT >= 26) {
editText.setRawInputType(InputType.TYPE_NULL)
editText.inputType = InputType.TYPE_CLASS_NUMBER
editText.showSoftInputOnFocus = false
editText.setOnClickListener {
hideKeyboard()
}
try {
val f = TextView::class.java.getDeclaredField("mCursorDrawableRes")
f.isAccessible = true
f.set(editText, R.drawable.cursor)
} catch (ignored: Exception) {
}
} else if (Build.VERSION.SDK_INT >= 24) {
editText.inputType = InputType.TYPE_CLASS_NUMBER
editText.showSoftInputOnFocus = false
editText.setOnClickListener {
hideKeyboard()
}
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER)
editText.setTextIsSelectable(true)
} else {
if (Build.VERSION.SDK_INT >= 21 ) {
editText.showSoftInputOnFocus = false
} else if (Build.VERSION.SDK_INT >= 11) {
editText.setRawInputType(InputType.TYPE_CLASS_TEXT)
editText.isCursorVisible = true
editText.setTextIsSelectable(true)
} else {
editText.setRawInputType(InputType.TYPE_NULL)
editText.isFocusable = true
}
try {
val f = TextView::class.java.getDeclaredField("mCursorDrawableRes")
f.isAccessible = true
f.set(editText, R.drawable.cursor)
} catch (ignored: Exception) {
ignored.printStackTrace()
}
}
}
布局文件中的 EditText
<EditText
android:layout_width="@dimen/_25sdp"
android:layout_height="@dimen/_25sdp"
android:cursorVisible="true"
android:digits="0123456789"
android:gravity="center"
android:imeOptions="actionNext"
android:longClickable="false"
android:maxLength="1"
android:singleLine="true"
android:textColor="#000000"
android:textSize="@dimen/_16sdp"
tools:text="0" />