java.lang.ClassCastException:android.inputmethodservice.KeyboardView无法转换为android.view.ViewGroup [英] java.lang.ClassCastException: android.inputmethodservice.KeyboardView cannot be cast to android.view.ViewGroup
问题描述
尝试在键盘视图内使用其他布局(如在xml文件中)时遇到此错误.我不希望我的键盘布局像普通键盘一样具有行和键,相反,我希望它在任何父布局的顶部两侧都具有两个按钮.另外,我还需要在键盘视图中放置一个布局以包含我的绘图画布.
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.
推荐答案
您会收到此错误,因为KeyboardView扩展了View,该View不能包含ViewGroup之类的子级.
You get the error because KeyboardView extends View , which cannot contain children like ViewGroup.
要使其正常工作,您需要使用键盘布局创建一个单独的布局文件,然后将其添加到KeyboardView中,如下所示:
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
这篇关于java.lang.ClassCastException:android.inputmethodservice.KeyboardView无法转换为android.view.ViewGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!