提问人:Jdusanter 提问时间:11/13/2023 更新时间:11/13/2023 访问量:10
在 InputMethodService 中切换键盘显示新键盘太大
Switching keyboard in InputMethodService display a too large new keyboard
问:
我正在尝试使用 KeyboardView 和 InputMethodService 实现自定义键盘。
我想要一个按钮,可以将我的键盘切换为字母键盘或特殊字符键盘。
但是当我切换键盘时,我的第二个键盘的宽度总是扩大,不适合宽度屏幕。
所以我创建了两个xml文件
首先是我的字母键盘(keys_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="4dp"
android:keyHeight="60dp"
android:verticalGap="4dp"
>
<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="52" android:keyLabel="4"/>
<Key android:codes="53" android:keyLabel="5"/>
<Key android:codes="54" android:keyLabel="6"/>
<Key android:codes="55" android:keyLabel="7"/>
<Key android:codes="56" android:keyLabel="8"/>
<Key android:codes="57" android:keyLabel="9"/>
<Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="97" android:keyLabel="a" android:keyEdgeFlags="left"/>
<Key android:codes="122" android:keyLabel="z"/>
<Key android:codes="101" android:keyLabel="e"/>
<Key android:codes="114" android:keyLabel="r"/>
<Key android:codes="116" android:keyLabel="t"/>
<Key android:codes="121" android:keyLabel="y"/>
<Key android:codes="117" android:keyLabel="u"/>
<Key android:codes="105" android:keyLabel="i"/>
<Key android:codes="111" android:keyLabel="o"/>
<Key android:codes="112" android:keyLabel="p"/>
<Key android:codes="-5" android:keyIcon="@drawable/baseline_backspace_24" android:isRepeatable="true" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/>
<Key android:codes="115" android:keyLabel="s"/>
<Key android:codes="100" android:keyLabel="d"/>
<Key android:codes="102" android:keyLabel="f"/>
<Key android:codes="103" android:keyLabel="g"/>
<Key android:codes="104" android:keyLabel="h"/>
<Key android:codes="106" android:keyLabel="j"/>
<Key android:codes="107" android:keyLabel="k"/>
<Key android:codes="108" android:keyLabel="l"/>
<Key android:codes="109" android:keyLabel="m"/>
<Key android:codes="-4" android:keyIcon="@drawable/baseline_keyboard_return_24" android:keyEdgeFlags="right"/>
<!-- <Key android:codes="35,64" android:keyLabel="\# \@" android:keyEdgeFlags="right"/>-->
</Row>
<Row>
<Key android:codes="-1" android:isModifier="true" android:keyIcon="@drawable/shift_outlined" android:keyEdgeFlags="left"/>
<Key android:codes="119" android:keyLabel="w"/>
<Key android:codes="120" android:keyLabel="x"/>
<Key android:codes="99" android:keyLabel="c"/>
<Key android:codes="118" android:keyLabel="v"/>
<Key android:codes="98" android:keyLabel="b"/>
<Key android:codes="110" android:keyLabel="n"/>
<Key android:codes="63" android:keyLabel="\?"/>
<Key android:codes="44" android:keyLabel=","/>
<Key android:codes="46" android:keyLabel="."/>
<Key android:codes="-1" android:isModifier="true" android:keyIcon="@drawable/shift_outlined" android:keyEdgeFlags="right"/>
<!-- <Key android:codes="63,33,58" android:keyLabel="\? ! :" android:keyEdgeFlags="right"/> -->
<!-- <Key android:codes="47" android:keyLabel="/" android:keyWidth="10%p" />-->
</Row>
<Row>
<Key android:codes="1339" android:isModifier="true" android:keyLabel="!#1" android:keyEdgeFlags="left"/>
<Key android:codes="1340" android:keyLabel="@string/key_label_mc_do"/>
<Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="60%p" android:isRepeatable="true"/>
<Key android:codes="1341" android:keyIcon="@drawable/baseline_keyboard_arrow_left_24" />
<Key android:codes="1342" android:keyIcon="@drawable/baseline_keyboard_arrow_right_24" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>
然后是我的特辑角色布局(keys_special_characters_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="4dp"
android:keyHeight="60dp"
android:verticalGap="4dp"
>
<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="52" android:keyLabel="4"/>
<Key android:codes="53" android:keyLabel="5"/>
<Key android:codes="54" android:keyLabel="6"/>
<Key android:codes="55" android:keyLabel="7"/>
<Key android:codes="56" android:keyLabel="8"/>
<Key android:codes="57" android:keyLabel="9"/>
<Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="43" android:keyLabel="+" android:keyEdgeFlags="left"/>
<Key android:codes="120" android:keyLabel="x"/>
<Key android:codes="247" android:keyLabel="÷"/>
<Key android:codes="61" android:keyLabel="="/>
<Key android:codes="47" android:keyLabel="/"/>
<Key android:codes="95" android:keyLabel="_"/>
<Key android:codes="36" android:keyLabel="€"/>
<Key android:codes="156" android:keyLabel="£"/>
<Key android:codes="165" android:keyLabel="¥"/>
<Key android:codes="8361" android:keyLabel="₩"/>
<Key android:codes="-5" android:keyIcon="@drawable/baseline_backspace_24" android:isRepeatable="true" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="64" android:keyLabel="@string/key_arobase" android:keyEdgeFlags="left"/>
<Key android:codes="35" android:keyLabel="#"/>
<Key android:codes="36" android:keyLabel="$"/>
<Key android:codes="37" android:keyLabel="%"/>
<Key android:codes="94" android:keyLabel="^"/>
<Key android:codes="38" android:keyLabel="@string/key_ampersand"/>
<Key android:codes="42" android:keyLabel="*"/>
<Key android:codes="40" android:keyLabel="("/>
<Key android:codes="41" android:keyLabel=")"/>
<Key android:codes="-4" android:keyIcon="@drawable/baseline_keyboard_return_24" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="-1" android:isModifier="true" android:keyIcon="@drawable/shift_outlined" android:keyEdgeFlags="left"/>
<Key android:codes="45" android:keyLabel="-"/>
<Key android:codes="39" android:keyLabel="'"/>
<Key android:codes="34" android:keyLabel="@string/key_quote"/>
<Key android:codes="58" android:keyLabel=":"/>
<Key android:codes="59" android:keyLabel=";"/>
<Key android:codes="33" android:keyLabel="!"/>
<Key android:codes="63" android:keyLabel="\?"/>
<Key android:codes="44" android:keyLabel=","/>
<Key android:codes="46" android:keyLabel="."/>
<Key android:codes="-1" android:isModifier="true" android:keyIcon="@drawable/shift_outlined" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="1339" android:isModifier="true" android:keyLabel="ABC" android:keyEdgeFlags="left"/>
<Key android:codes="1340" android:keyLabel="@string/key_label_mc_do"/>
<Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="60%p" android:isRepeatable="true"/>
<Key android:codes="1341" android:keyIcon="@drawable/baseline_keyboard_arrow_left_24" />
<Key android:codes="1342" android:keyIcon="@drawable/baseline_keyboard_arrow_right_24" android:keyEdgeFlags="right"/>
</Row>
</Keyboard>
然后在我的 InputMethodService 中,我这样定义键盘:
private var keyboardView: KeyboardView? = null
private var currentKeyboard: Keyboard? = null
private var letterKeyboard: Keyboard? = null
private var specialCharKeyboard: Keyboard? = null
override fun onCreate() {
super.onCreate()
letterKeyboard = Keyboard(this, R.xml.keys_layout)
specialCharKeyboard = Keyboard(this, R.xml.keys_special_characters_layout)
currentKeyboard = letterKeyboard
}
override fun onCreateInputView(): View {
keyboardView = layoutInflater.inflate(R.layout.keyboard_view, null) as KeyboardView
keyboardView?.keyboard = currentKeyboard
keyboardView?.setOnKeyboardActionListener(this)
keyboardView?.isPreviewEnabled = false
return keyboardView!!
}
override fun onKey(primaryCode: Int, keyCodes: IntArray) {
SPECIAL_CHARACTERS_KEY_CODE -> {
toggleKeyboard()
}
}
private fun toggleKeyboard() {
currentKeyboard = if (currentKeyboard === letterKeyboard) {
specialCharKeyboard
} else {
letterKeyboard
}
keyboardView?.keyboard = currentKeyboard
}
切换很好,但是当我切换到新键盘时,我的键盘太大了,如下图所示:
任何想法,为什么以及如何解决这个问题?
提前非常感谢,祝你有美好的一天!
答: 暂无答案
评论