Android InputMethodEditor layout file. Non-Key/Row elements in the XML?

别说谁变了你拦得住时间么 提交于 2021-01-28 07:09:38

问题


I'm trying to implement a keyboard with a TextView that updates with each keypress, but it won't show up on the screen. It should appear above the top row of letters.

When I run the keyboard, the keys show up with an extra row on top but nothing inside

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:id="@+id/keyboardView"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="60dp"
    >
    <Row>
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:text="@string/text_view_default"
            android:visibility="visible"/>
            <!--android:layout_height="50dp"-->
    </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="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>
    .
    .
    .
</Keyboard>

回答1:


You will have to change your keyboard layout to contain your TextView and KeyboardView.

1) change IME layout as below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TextView
            android:id="@+id/tvKeyboard"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:gravity="center"
            android:padding="10dp"
            android:scaleType="fitEnd"
            android:src="@drawable/keyboard_icon" />

    </RelativeLayout>

    <android.inputmethodservice.KeyboardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/keyboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:keyPreviewLayout ="@layout/preview"
    />

</LinearLayout>

2) In your IME class make a few changes:

@Override 
public View onCreateInputView() {

    final View root = getLayoutInflater().inflate(R.layout.idee_keyboard_layout, null);

    TextView tvKeyboard = (TextView) root.findViewById(R.id.tvRevertKeyboard);

    tvKeyboard.setText("set your text here");

    kv = (KeyboardView) root.findViewById(R.id.keyboard);

    keyboard = new Keyboard(this, R.xml.qwerty);
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);
    return root;
} 

Done.




回答2:


Nesting the TextView in the ReleativeLayout and that RelativeLayout with the KeyboardView in a LinearLayout resulted in the TextView being displayed. A couple more bugs came up, but after rebuilding the project, they subsided. Thanks for the help!



来源:https://stackoverflow.com/questions/34215537/android-inputmethodeditor-layout-file-non-key-row-elements-in-the-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!