Android 自定义键盘从 Activity 更改

标签 android android-softkeyboard

我制作了一个自定义键盘应用程序:

public class SimpleIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener
{
    private KeyboardView kv;
    private Keyboard keyboard;
    private boolean caps = false;

    @Override
    public View onCreateInputView()
    {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }
}

XML:

<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/key_image"
android:background="@drawable/keyboard_image"
android:keyPreviewLayout ="@layout/preview"
/>

现在,有一个设置键,当用户按下它时,新的Activity将打开。此 Activity 有两个按钮。第一个应该更改键盘背景图像,第二个应该更改按钮背景图像。

我正在尝试一些事情,但我无法做到。有人可以告诉我我应该怎么做吗?

谢谢。

最佳答案

我已经找到解决办法了。通过从 Activity 更改主题可以轻松更改键盘:

themePreferencesEditor.putInt(THEME_KEY, 2);
themePreferencesEditor.commit();

然后在 IME 中:

@Override
    public View onCreateInputView()
    {
        pre = getSharedPreferences("CUSTOM_KEYBOARD_PREFERENCES", 1);
        theme = pre.getInt(THEME_KEY, 1);
        switch (theme)
        {
            case 1:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
                break;
            case 2:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard1, null);
                break;
            case 3:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard2, null);
                break;
            case 4:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard3, null);
                break;
            case 5:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard4, null);
                break;
            case 6:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard5, null);
                break;
            default:
                kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
                break;
        }
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this);
        return kv;
    }

    @Override
    public void onStartInputView(EditorInfo info, boolean restarting)
    {
        super.onStartInputView(info, restarting);
        setInputView(onCreateInputView());
    }

关于Android 自定义键盘从 Activity 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29277390/

相关文章:

java - 为什么我已经注册了警报却收不到接近警报?

android - 通过Data类发送类对象

android - 我需要在我杀死或从 Android 中最近的应用程序中停止我的服务

android - 如何避免元素在 RelativeLayout 上重叠显示软键盘

android - 如何在操作栏中显示 EditText 时聚焦并显示软键盘?

android - 在 Android 4.x 上按下时 Listview 中的 EditText 失去焦点

android - 对于 Android 应用程序,哪个是最好的 firebase 或 php 和 json with mysql?

android - 如何在 Windows 操作系统中查看 Android 源代码?

android - 如何触发Android键盘上的shift键?

android - 从 ViewPager fragment 隐藏 Android 应用程序中的软键盘