【问题标题】:Change Layout of Keyboard Dynamically动态更改键盘布局
【发布时间】:2012-12-20 20:21:22
【问题描述】:

我正在创建自定义键盘。我从Here 那里得到了一个非常有用的演示。我想创建键盘的多个主题,所以我为键盘创建了另一个布局,但现在的问题是我不知道如何设置当前键盘的布局或必须重新加载键盘或必须做其他事情..改变键盘的设计没有任何想法。

我的概念是用户必须从活动中选择键盘主题,键盘设计会改变。

任何人都可以帮助我或有任何想法解决这个问题..?

【问题讨论】:

    标签: android keyboard android-softkeyboard ime


    【解决方案1】:

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

    当键盘第一次加载时调用 onCreateInputView()。之后,每次打开键盘时都会调用 onStartInputView(EditorInfo 属性,布尔重启)。

    所以,现在键盘(主题)的布局必须在 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());
    }
    

    【讨论】:

    • 如何处理自定义布局的点击事件。
    • 感谢@Yog Guru :)
    • 这种重新创建整个输入视图的方法特别好,因为 KeyboardView 错误地缓存了 keyWidth,这使得使用简单的 setKeyboard 切换布局变得不可能。
    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多