Android自定义键盘布局切换

时间:2018-10-30 04:35:02

标签: android layout keyboard

我想开发自己的键盘。我按照here

中的解释进行操作

当我仅使用一种键盘布局时,这就像一种魅力。如果我要使用两种布局并在它们之间切换,则布局会发生奇怪的情况。

letter keyboard pic1中的一切都很好,现在我切换到符号键盘布局。 symbols keyboard 但是在这里我们可以看到右键按钮从右侧边框退出。 现在,将键盘布局切换回字母键盘,看看 右列完全消失。 letters keyboard 2 如何解决此问题?

键盘布局看起来像这样

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:keyHeight="60dp"
    android:horizontalGap="3px"
    android:verticalGap="3px"
    android:keyTextColor="@color/colorWhite"
    android:keyBackground="@color/colorPrimaryDark">
    <Row>
        <Key android:codes="1081" android:keyLabel="й" android:keyEdgeFlags="left"/>
        <Key android:codes="1094" android:keyLabel="ц"/>
        <Key android:codes="1091" android:keyLabel="у" android:popupKeyboard="@xml/popupkeyboard_template" android:popupCharacters="@string/alternates_for_y"/>
        <Key android:codes="1082" android:keyLabel="к"/>
        <Key android:codes="1077" android:keyLabel="е" android:popupKeyboard="@xml/popupkeyboard_template" android:popupCharacters="@string/alternates_for_e"/>
        <Key android:codes="1085" android:keyLabel="н"/>
        <Key android:codes="1075" android:keyLabel="г"/>
        <Key android:codes="1096" android:keyLabel="ш"/>
        <Key android:codes="1097" android:keyLabel="щ"/>
        <Key android:codes="1079" android:keyLabel="з"/>
        <Key android:codes="1093" android:keyLabel="х" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="1092" android:keyLabel="ф" android:keyEdgeFlags="left"/>
        <Key android:codes="1099" android:keyLabel="ы"/>
        <Key android:codes="1074" android:keyLabel="в"/>
        <Key android:codes="1072" android:keyLabel="а" android:popupKeyboard="@xml/popupkeyboard_template" android:popupCharacters="@string/alternates_for_a"/>
        <Key android:codes="1087" android:keyLabel="п"/>
        <Key android:codes="1088" android:keyLabel="р"/>
        <Key android:codes="1086" android:keyLabel="о"/>
        <Key android:codes="1083" android:keyLabel="л"/>
        <Key android:codes="1076" android:keyLabel="д"/>
        <Key android:codes="1078" android:keyLabel="ж"/>
        <Key android:codes="1101" android:keyLabel="э" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="-1"  android:keyIcon="@drawable/ic_arrow_drop_up_black_24dp" android:keyEdgeFlags="left"/>
        <Key android:codes="1103" android:keyLabel="я"/>
        <Key android:codes="1095" android:keyLabel="ч"/>
        <Key android:codes="1089" android:keyLabel="с" android:popupKeyboard="@xml/popupkeyboard_template" android:popupCharacters="@string/alternates_for_c"/>
        <Key android:codes="1084" android:keyLabel="м"/>
        <Key android:codes="1080" android:keyLabel="и"/>
        <Key android:codes="1090" android:keyLabel="т"/>
        <Key android:codes="1100" android:keyLabel="ь" android:popupKeyboard="@xml/popupkeyboard_template" android:popupCharacters="@string/alternates_for_hard_sign"/>
        <Key android:codes="1073" android:keyLabel="б"/>
        <Key android:codes="1102" android:keyLabel="ю"/>
        <Key android:codes="-5" android:keyIcon="@drawable/ic_backspace" android:keyWidth="20%p" android:isRepeatable="true" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="0" android:keyLabel="\?123" android:keyWidth="20%p" android:keyEdgeFlags="left"/>
        <Key android:codes="47" android:keyLabel="/"/>
        <Key android:codes="32" android:keyLabel="@string/keyboard_space" android:keyWidth="40%p" android:isRepeatable="true"/>
        <Key android:codes="46" android:keyLabel="."/>
        <Key android:codes="-4" android:keyBackground="@color/colorPrimary" android:keyLabel="@string/keyboard_search" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard> 

symbols_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:keyHeight="60dp"
    android:horizontalGap="3px"
    android:verticalGap="3px"
    android:keyTextColor="@color/colorWhite"
    android:keyBackground="@color/colorPrimaryDark">
    <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="64" android:keyLabel="\@" android:keyEdgeFlags="left"/>
        <Key android:codes="35" android:keyLabel="#"/>
        <Key android:codes="36" android:keyLabel="$"/>
        <Key android:codes="37" android:keyLabel="%"/>
        <Key android:codes="38" android:keyLabel="&amp;"/>
        <Key android:codes="45" android:keyLabel="-"/>
        <Key android:codes="43" android:keyLabel="+"/>
        <Key android:codes="40" android:keyLabel="("/>
        <Key android:codes="41" android:keyLabel=")"/>
        <Key android:codes="91" android:keyLabel="[" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="42" android:keyLabel="*" android:keyEdgeFlags="left"/>
        <Key android:codes="34" android:keyLabel="&quot;"/>
        <Key android:codes="39" android:keyLabel="'"/>
        <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="93" android:keyLabel="]"/>
        <Key android:codes="-5" android:keyIcon="@drawable/ic_backspace" android:keyWidth="20%p" android:isRepeatable="true" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="0" android:keyLabel="АБВ" android:keyWidth="20%p" android:keyEdgeFlags="left"/>
        <Key android:codes="47" android:keyLabel="/"/>
        <Key android:codes="32" android:keyLabel="@string/keyboard_space" android:keyWidth="40%p" android:isRepeatable="true"/>
        <Key android:codes="46" android:keyLabel="."/>
        <Key android:codes="-4" android:keyBackground="@color/colorPrimary" android:keyLabel="@string/keyboard_search" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

我这样切换键盘布局

        case 0:
            if (iKeyboardType == 1) {
                keyboard = new Keyboard(this, R.xml.symbols_layout);
                iKeyboardType = 2;
            }
            else if (iKeyboardType == 2){
                keyboard = new Keyboard(this, R.xml.keys_layout);
                iKeyboardType = 1;
            }
            keyboardView.setKeyboard(keyboard);
            keyboardView.invalidateAllKeys();
            break;

2 个答案:

答案 0 :(得分:0)

我找不到任何正确的解决方案,所以我用自己的方式做到了。如果两个布局中的键大小相同,则一切正常,并且键不会移到外面。也许对某人有帮助。

答案 1 :(得分:0)

遇到类似问题,但不确定是否相同。我所做的是确保行中每个按钮的android keywidth不超过100%p

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="0dp"
    android:verticalGap="0dp"
    android:keyHeight="60dp">

    <Row>
    <Key android:keyLabel="1"
        android:keyEdgeFlags="left"
        android:codes="49" android:keyWidth="10%p"  />
    <Key android:keyLabel="2"
        android:codes="50" android:keyWidth="10%p"  />
    <Key android:keyLabel="3"
        android:codes="51" android:keyWidth="10%p"  />
    <Key android:keyLabel="4"
        android:codes="52" android:keyWidth="10%p"  />
    <Key android:keyLabel="5"
        android:codes="53" android:keyWidth="10%p"  />
    <Key android:keyLabel="6"
        android:codes="54" android:keyWidth="10%p"  />
    <Key android:keyLabel="7"
        android:codes="55" android:keyWidth="10%p"  />
    <Key android:keyLabel="8"
        android:codes="56" android:keyWidth="10%p"  />
    <Key android:keyLabel="9"
        android:codes="57" android:keyWidth="10%p"  />
    <Key android:keyLabel="0"
        android:keyEdgeFlags="right"
        android:codes="48" android:keyWidth="10%p"  />
</Row>

    <Row>
        <Key android:keyLabel="q" android:keyEdgeFlags="left" android:codes="113" android:keyWidth="10%p" />
        <Key android:keyLabel="w" android:codes="119" android:keyWidth="10%p" />
        <Key android:keyLabel="e" android:codes="101" android:keyWidth="10%p" />
        <Key android:keyLabel="r" android:codes="114" android:keyWidth="10%p" />
        <Key android:keyLabel="t" android:codes="116" android:keyWidth="10%p" />
        <Key android:keyLabel="y" android:codes="121" android:keyWidth="10%p" />
        <Key android:keyLabel="u" android:codes="117" android:keyWidth="10%p" />
        <Key android:keyLabel="i" android:codes="105" android:keyWidth="10%p" />
        <Key android:keyLabel="o" android:codes="111" android:keyWidth="10%p" />
        <Key android:keyLabel="p" android:keyEdgeFlags="right" android:codes="112" android:keyWidth="10%p" />
    </Row>

    <Row>
        <Key android:keyLabel="a" android:keyEdgeFlags="left" android:codes="97" android:keyWidth="11.1%p" />
        <Key android:keyLabel="s" android:codes="115" android:keyWidth="11.1%p" />
        <Key android:keyLabel="d" android:codes="100" android:keyWidth="11.1%p" />
        <Key android:keyLabel="f" android:codes="102" android:keyWidth="11.1%p" />
        <Key android:keyLabel="g" android:codes="103" android:keyWidth="11.1%p" />
        <Key android:keyLabel="h" android:codes="104" android:keyWidth="11.1%p" />
        <Key android:keyLabel="j" android:codes="106" android:keyWidth="11.1%p" />
        <Key android:keyLabel="k" android:codes="107" android:keyWidth="11.1%p" />
        <Key android:keyLabel="l" android:codes="108" android:keyWidth="11.1%p" android:keyEdgeFlags="right" />
<!--        <Key android:keyLabel="\#" android:keyEdgeFlags="right" android:codes="35" />-->
    </Row>

    <Row>
        <Key android:keyLabel="CAP" android:keyEdgeFlags="left" android:codes="-1" android:keyWidth="12%p" />
        <Key android:keyLabel="z" android:codes="122" android:keyWidth="10.8%p" />
        <Key android:keyLabel="x" android:codes="120" android:keyWidth="10.8%p" />
        <Key android:keyLabel="c" android:codes="99" android:keyWidth="10.8%p" />
        <Key android:keyLabel="v" android:codes="118" android:keyWidth="10.8%p" />
        <Key android:keyLabel="b" android:codes="98" android:keyWidth="10.8%p" />
        <Key android:keyLabel="n" android:codes="110" android:keyWidth="10.8%p" />
        <Key android:keyLabel="m" android:codes="109" android:keyWidth="10.8%p" />
<!--        <Key android:keyLabel="." android:codes="46" />-->
<!--        <Key android:keyLabel="\:" android:keyWidth="10%p" android:codes="58" />-->
        <Key android:keyLabel="DEL"  android:keyEdgeFlags="right" android:isRepeatable="true" android:keyWidth="12%p" android:codes="-5" />
<!--        <Key android:keyLabel="\! :" android:keyEdgeFlags="right" android:codes="33, 58" />-->
<!--        <Key android:keyLabel="\? ! :" android:keyEdgeFlags="right" android:codes="63, 33, 58" />-->
    </Row>

    <Row android:rowEdgeFlags="bottom">

<!--        <Key android:keyLabel="/" android:keyWidth="10%p"  android:keyEdgeFlags="left" android:codes="47" />-->
<!--        <Key android:keyLabel="\!" android:keyWidth="10%p" android:codes="33" />-->
        <Key android:keyEdgeFlags="left" android:keyLabel="!#1" android:codes="-2" android:keyWidth="14%p"/>
        <Key android:keyLabel="," android:codes="44" android:keyWidth="11%p" />
<!--        <Key android:keyLabel="\@"  android:keyWidth="10%p" android:codes="64" />-->
        <Key android:keyLabel="SPACE" android:keyWidth="50%p" android:isRepeatable="true" android:codes="32" />
        <Key android:keyLabel="." android:codes="46" android:keyWidth="11%p" />
<!--        <Key android:keyLabel="\?" android:keyWidth="10%p" android:codes="63" />-->
        <Key android:keyLabel="DONE" android:keyEdgeFlags="right" android:codes="-4" android:keyWidth="14%p" />

    </Row>

</Keyboard>