在 android 中使虚拟键盘可滚动

Making a Virtual Keyboard scrollable in android

我已经成功创建了一个 IMS(InputMangerService) 虚拟键盘,现在我想让它可以滚动,就像 google 表情符号键盘一样。

至少可以使用候选视图,并且google已经用他们自己的表情符号键盘实现了。

但是我不知道该怎么做。有什么想法吗?

编辑 1:进一步的测试和研究没有产生任何结果。事实上,我能找到的唯一可滚动键盘示例是 google 自己的候选视图和一个使用 activity 中的对话框视图而不是软键盘制作的示例。

所以我的新问题是:是否有可能创建一个可滚动的虚拟键盘?

我找到了解决方案:

您需要创建一个没有按钮或行的虚拟键盘,然后将该键盘的候选视图设置为新的滚动视图,然后显示候选视图。从那里您可以通过编程方式将视图添加到滚动视图以创建您想要的键盘。

public class MyKeyboardService extends InputMethodService implements KeyboardView.OnKeyboardActionListener
{
    private KeyboardView viewOfKeyboard;
    private Keyboard theKeyboardLayout;
    private HorizontalScrollView scrollingKeyboard;


    @Override
    public View onCreateInputView()
    {
       viewOfKeyboard =  keyboardView)getLayoutInflater().inflate(R.layout.keyboard1, null);
       theKeyboardLayout = new Keyboard(this, R.xml.keyboardlayout);
       //keyboardlayout.xml contains no rows or keys.
       viewOfKeyboard.setKeyboard(theKeyboardLayout);


       setCandidatesViewShown(true);
       //Sets the candidate view to be always shown.
       return viewOfKeyboard;
    }

    public View onCreateCandidatesView()
    {
       scrollingKeyboard = new HorizontalScrollView(this);
       scrollingKeyboard.addView(new Button(this));
       return scrollingKeyboard;
    }
}