【问题标题】:java.lang.ClassCastException: android.inputmethodservice.KeyboardView cannot be cast to android.view.ViewGroupjava.lang.ClassCastException: android.inputmethodservice.KeyboardView 不能转换为 android.view.ViewGroup
【发布时间】:2017-10-02 18:39:41
【问题描述】:

在我的 xml 文件中尝试使用键盘视图中的其他布局时出现此错误。我不希望我的键盘布局像普通键盘那样具有行和键,而是希望它在任何父布局的顶部两侧都有两个按钮。我还需要在键盘视图中进行布局以包含我的绘图画布。

我也尝试过这样的布局:

<?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>

然后尝试像这样加载它:

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

任何帮助或教程链接将不胜感激。

【问题讨论】:

  • 这个运气好吗?我有同样的问题。

标签: android android-softkeyboard custom-keyboard


【解决方案1】:

您收到错误是因为 KeyboardView 扩展了 View ,它不能包含像 ViewGroup 这样的子项。

为了使其正常工作,您需要使用键盘布局创建一个单独的布局文件,然后将其添加到 KeyboardView,如下所示:

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

有关如何自定义键盘的完整教程:https://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

【讨论】:

  • KeyPreviewLayout 只接受文本视图。当我添加任何其他布局时应用程序崩溃。我想添加一个带有两个按钮的父布局。
  • 查看教程的第 6 部分
  • 我在那里只看到一个教程。可以分享第 6 部分的链接吗?
  • 我无法添加任何其他视图来代替这些带有键的键盘行。我想在键盘屏幕上画画。我应该将键盘视为画布,可以在动作触摸上绘图。
猜你喜欢
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多