问题描述
我创建了一个自定义键盘,但没有在显示系统关键字的编辑文本中显示。我
这是我的代码:
BasicOnKeyboardActionListener
import android.app.Activity
import android.app.Dialog
import android.inputmethodservice.KeyboardView
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import androidx.core.view.isGone
import androidx.core.view.isVisible
class BasicOnKeyboardActionListener(
private val activity: Activity,private val keyboardView: KeyboardView
) : KeyboardView.OnKeyboardActionListener {
override fun swipeRight() {
}
override fun onPress(p0: Int) {
}
override fun onRelease(p0: Int) {
}
override fun swipeLeft() {
}
override fun swipeUp() {
}
override fun swipeDown() {
}
override fun onKey(primaryCode: Int,p1: IntArray?) {
val eventTime = System.currentTimeMillis()
val event = KeyEvent(
eventTime,eventTime,KeyEvent.ACTION_DOWN,primaryCode,KeyEvent.FLAG_SOFT_KEYBOARD or KeyEvent.FLAG_KEEP_TOUCH_MODE
)
activity.dispatchKeyEvent(event)
}
override fun onText(p0: CharSequence?) {
}
fun registerEditText(edittext: EditText?) {
with(edittext) {
this?.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(
s: CharSequence,start: Int,count: Int,after: Int
) {
}
override fun onTextChanged(
s: CharSequence,before: Int,count: Int
) {
}
override fun afterTextChanged(editable: Editable) {
val mS = editable.subSequence(0,editable.length)
if (mS.toString() == "" || mS.toString() == null) {
return
}
if (editable.isNotEmpty() && mS.toString().contains("=")) {
editable.replace(editable.length - 1,editable.length,"")
}
}
})
this!!.onFocuschangelistener = View.OnFocuschangelistener { v,hasFocus ->
if (hasFocus) showCustomKeyboard(v) else hideCustomKeyboard()
}
setonTouchListener { view,event ->
val editableText = view as EditText
val inputType = editableText.inputType
with(editableText) {
setInputType(InputType.TYPE_NULL)
onTouchEvent(event)
setInputType(inputType)
}
true
}
}
}
private fun hideCustomKeyboard() {
with(keyboardView) {
isGone = true
isEnabled = false
}
}
private fun showCustomKeyboard(view: View?) {
with(keyboardView) {
isVisible = true
isEnabled = true
}
view?.let {
activity.getwindow()!!.setSoftInputMode(WindowManager.LayoutParams.soFT_INPUT_STATE_ALWAYS_HIDDEN)
}
}
}
这里是bottomsheetdialogfragmentListDialogFragment:
这是我的bottomsheetdialogfragmentListDialogFragment,我在其中声明了主要活动edittext。
import android.app.Dialog
import android.inputmethodservice.Keyboard
import android.os.Bundle
import android.view.View
import android.widget.EditText
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.android.synthetic.main.keyboard_view.*
// Todo: Customize parameter argument names
const val ARG_ITEM_COUNT = "item_count"
/**
*
* A fragment that shows a list of items as a modal bottom sheet.
*
* You can show this modal bottom sheet from your activity like this:
* <pre>
* bottomsheetdialogfragmentListDialogFragment.newInstance(30).show(supportFragmentManager,"dialog")
* </pre>
*/
class bottomsheetdialogfragmentListDialogFragment : BottomSheetDialogFragment() {
private lateinit var edit: EditText
private val keyboardListener by lazy {
BasicOnKeyboardActionListener(activity ?: requireActivity(),keyboard_view)
}
private val customKeyboard by lazy {
Keyboard(requireContext(),R.xml.number_pad)
}
override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
super.onViewCreated(view,savedInstanceState)
registerKeyboardEvent()
setCustomKeyboard()
edit = view.findViewById(R.id.board) as EditText
}
private fun setCustomKeyboard() {
with(keyboard_view) {
isPreviewEnabled = false
keyboard = customKeyboard
setonKeyboardActionListener(keyboardListener)
}
}
private fun registerKeyboardEvent() {
with(keyboardListener) {
registerEditText(edit)
}
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a.customkeyboard">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".BasicOnKeyboardActionListener"
android:label="Keyboard"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod"/>
</intent-filter>
<Meta-data
android:name="android.view.im"
android:resource="@xml/method"/>
</service>
</application>
</manifest>
键盘 Xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="20%p"
android:horizontalGap="5dp"
android:verticalGap="5dp"
android:keyHeight="60dp">
<Row>
<Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left"/>
<Key android:codes="56" android:keyLabel="8"/>
<Key android:codes="57" android:keyLabel="9"/>
<Key android:codes="10" android:keyLabel="Next"/>
<Key android:keyLabel="13/16" android:keyOutputText="13/16"/>
<Key android:keyOutputText="7/8" android:keyLabel="7/8"/>
<Key android:keyOutputText="15/16" android:keyLabel="15/16"/>
<Key android:codes="-4" android:keyLabel="C" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="52" android:keyLabel="4" android:keyEdgeFlags="left"/>
<Key android:codes="53" android:keyLabel="5"/>
<Key android:codes="54" android:keyLabel="6"/>
<Key android:codes="43" android:keyLabel="+"/>
<Key android:keyOutputText="9/16" android:keyLabel="9/16"/>
<Key android:keyOutputText="5/8" android:keyLabel="5/8"/>
<Key android:keyOutputText="11/16" android:keyLabel="11/16"/>
<Key android:keyOutputText="3/4" android:keyLabel="3/4" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
<Key android:codes="50" android:keyLabel="2"/>
<Key android:codes="51" android:keyLabel="3"/>
<Key android:codes="45" android:keyLabel="-"/>
<Key android:keyOutputText="5/16" android:keyLabel="5/16"/>
<Key android:keyOutputText="3/8" android:keyLabel="3/8"/>
<Key android:keyOutputText="7/16" android:keyLabel="7/16"/>
<Key android:keyOutputText="1/2" android:keyLabel="1/2" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="left"/>
<Key android:codes="46" android:keyLabel="."/>
<Key android:codes="-5" android:keyLabel="Back" />
<Key android:keyOutputText="1/16" android:keyLabel="1/16"/>
<Key android:keyOutputText="1/8" android:keyLabel="1/8"/>
<Key android:keyOutputText="3/16" android:keyLabel="3/16"/>
<Key android:keyOutputText="1/4" android:keyLabel="1/4" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)