java.lang.ClassCastException: android.inputmethodservice.KeyboardView cannot be cast to android.view.ViewGroup

隐身守侯 提交于 2019-12-11 14:41:49

问题


I am getting this error while trying to use other layouts inside keyboard view as in my xml file. I don't want my keyboard layout to have rows and keys as in normal keyboard instead i want it to have two buttons on either side at the top inside any parent layout. Also I need a layout inside keyboard view to contain my drawing canvas.

I have also tried to put in a layout like this:

<?xml version="1.0" encoding="utf-8"?>
<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:keyBackground="@drawable/square_rounded"
    android:background="#ffffff">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/mainLinearLayout">

    <RelativeLayout
        android:id="@+id/keyboardLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="Cancel"
            android:layout_alignParentLeft="true">
        </Button>

        <Button
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="Done"
            android:layout_alignParentRight="true">
        </Button>

        <LinearLayout
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </LinearLayout>
    </RelativeLayout>
</LinearLayout>
</android.inputmethodservice.KeyboardView>

and then tried loading it like:

       kv = (KeyboardView)getLayoutInflater()
        .inflate(R.layout.draw_signature, null);
        kv.setOnKeyboardActionListener(this);
        kv.setPreviewEnabled(false);
        return kv;

Any help or a tutorial link would be appreciated.


回答1:


You get the error because KeyboardView extends View , which cannot contain children like ViewGroup.

In order to make it work you need to create a separate layout file with your keyboard layout and then add it to the KeyboardView like this:

<android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    ...
    android:keyPreviewLayout ="@layout/your_keyboard_layout">

For full tutorial how to customize keyboard : https://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615



来源:https://stackoverflow.com/questions/43785328/java-lang-classcastexception-android-inputmethodservice-keyboardview-cannot-be

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