Android 自定义键盘更改自 Activity

Android Custom Keyboard changing from Activity

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

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());
    }