适用于Android的透明InputMethod

时间:2012-07-12 21:30:51

标签: android transparent

尝试制作透明的Android InputMethod - 即底层内容会显示在我正在开发的键盘上。

我已经能够将我传递给系统的视图透明 - 我认为 - 但我的视图下面似乎有一些纯白色的东西 - 并且模糊了底层内容。

绝对有可能,这些家伙会这样做:

https://play.google.com/store/apps/details?id=com.aitype.android.tablet.p&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5haXR5cGUuYW5kcm9pZC50YWJsZXQucCJd

3 个答案:

答案 0 :(得分:6)

我明白了!不确定这是你的游戏商店链接中的人是如何做到的,但这对我有用。此外,我意识到这篇文章已有一年多的历史了,但我仍在回答它,以防其他人在尝试创建透明键盘时发现这一点。

你视线下的“东西”实际上什么都没有 - 它是空的空间。你的键盘向上推动整个视图,为其高度腾出空间,留下空白空间。你透明的键盘让这个空白区域显示出来。

以下是解决方案:不是在onCreateInputView中返回视图,而是在onCreateCandidatesView中返回它。这是通常位于键盘上方并列出自动更正建议的视图。但是你将使用它来存放你的实际键盘。

您希望将键盘作为候选视图的原因是因为输入视图通常会推动基础视图。当通过android:windowSoftInputMode显示键盘时,各个应用程序可以决定他们想要表现的行为,输入视图尊重他们的偏好,但候选人视图总是使用adjustPan。

来自文档:“请注意,因为候选视图往往会被显示和隐藏很多,它不会像软输入视图那样影响应用程序UI:它永远不会导致应用程序窗口调整大小,仅如果需要让用户看到当前的焦点,可以将它们平移。“ http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html

因此,从onCreateCandidatesView返回透明视图,从onCreateInputView返回null并确保调用setCandidatesViewShown(true),以便显示候选视图(我在onWindowShown中调用它)。

答案 1 :(得分:0)

通常,InputMethodServices使用与当前绑定应用程序的背景颜色相同的背景颜色。如果你想让它变得透明,我认为你应该把它作为弹出窗口结构,而不是我认为的输入方法窗口。

答案 2 :(得分:0)

只有当您非常熟悉InputMethodService时,才可以通过java反射轻松实现全屏键盘布局的额外区域。

额外区域的ID名称为 fullscreenArea ,您可以获取区域的ID,然后findViewById()然后设置其背景。

在我完成练习之前,键盘看起来像这样:

before appearance

下面的页面上有一个巨大的空白。

所以之后是:

after

您可以看到下面的页面,其中包含EditText和其他显示的内容。

这是我的代码:

public static void makeKeyboardTransparent(InputMethodService service) {
    try {
        View decorView = service.getWindow().getWindow().getDecorView();
        final int viewId = fetchInternalRId("fullscreenArea");
        View fullscreenArea = decorView.findViewById(viewId);
        if (fullscreenArea != null) {
            modifyView(fullscreenArea);
            return;
        }
    } catch (Exception e) {
    }

    try {
        Class<?> superClass = service.getClass().getSuperclass();
        Field fullscreenAreaField = superClass.getDeclaredField("mFullscreenArea");
        fullscreenAreaField.setAccessible(true);
        View fullscreenArea = (View) fullscreenAreaField.get(service);
        if (fullscreenArea != null) {
            modifyView(fullscreenArea);
        }
    } catch (Exception e) {
    }
}

private static void modifyView(View fullscreenArea) {
    fullscreenArea.setBackgroundColor(Color.TRANSPARENT);
}

private static int fetchInternalRId(String name) throws Exception {
    Class<?> rIdClass = Class.forName("com.android.internal.R$id");
    return rIdClass.getDeclaredField(name).getInt(rIdClass);
}

我提供了两种方法来使空白区域透明,它们都在我的测试中运行良好,您只需将InputMethodService传递到makeKeyboardTransparent()并查看它可以做什么。