Android自定义键盘从活动更改

Android自定义键盘从活动更改,android,android-softkeyboard,Android,Android Softkeyboard,我制作了一个自定义键盘应用程序: public class SimpleIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView kv; private Keyboard keyboard; private boolean caps = false; @Override public View onCreat

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

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:


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

我在尝试一些事情,但我做不到。谁能告诉我该怎么做


谢谢。

我找到了解决办法。通过从“活动”更改键盘主题,可以轻松更改键盘:

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

setBackground应该适用于kb背景,我认为没有按键功能。实际上,当您想要超越绝对基础时,您需要将KeyboardView放在后面,编写一个自定义视图。它只是不是很灵活。
@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());
    }