动态更改键盘布局

4
我正在创建自定义键盘。我从这里得到了一个非常好的、有用的演示。我想创建多个主题的键盘,所以我为键盘创建了另一个布局,但现在的问题是我不知道如何设置当前键盘的布局,或者必须重新加载键盘,或者必须进行其他操作,来改变键盘的设计,我没有任何想法。
我的思路是用户可以从活动中选择键盘主题,然后键盘设计会更改。
有人可以帮助我或者有任何想法来解决这个问题吗?
1个回答

12

获得更改自定义键盘布局的解决方案。

当键盘初次加载 onCreateInputView() 被调用。此后,每次打开键盘时都会调用 onStartInputView(EditorInfo attribute, boolean restarting)。

因此,现在必须在 onCreateInputView() 中定义键盘(主题)的布局,如下所示:

public KeyboardView mInputView;
public View onCreateInputView() {

    SharedPreferences pre = getSharedPreferences("test", 1);
    int theme = pre.getInt("theme", 1);

    if(theme == 1)
    {
        this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input, null);
    }else
    {
        this.mInputView = (KeyboardView) this.getLayoutInflater().inflate(R.layout.input_2, null);

    }
    this.mInputView.setOnKeyboardActionListener(this);
    this.mInputView.setKeyboard(this.mQwertyKeyboard);
    return this.mInputView;
}

并在 onStartInputView 中执行此操作

public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);

    setInputView(onCreateInputView());
}

如何处理自定义布局的点击事件。 - Kaustubh
感谢 @Yog Guru :) - Siddhpura Amit
重新创建整个输入视图的方法特别好,因为KeyboardView错误地缓存了keyWidth,这使得使用简单的setKeyboard切换布局变得不可能。 - Boris Treukhov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接