【问题标题】:Rejusting UI with candidateview visible in custom keyboard使用自定义键盘中可见的候选视图重新调整 UI
【发布时间】:2022-03-01 17:53:21
【问题描述】:

我正在使用自定义键盘。我在 onCreateCandidatesView() 中设置了 setCandidatesViewShown(true) 函数,问题是 UI 没有正确调整。

任何帮助都会很棒..以下是我所做的

@Override 
public View onCreateCandidatesView() {      

    LayoutInflater li = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View wordBar = li.inflate(R.layout.wordbar, null);
    LinearLayout ll = (LinearLayout) wordBar.findViewById(R.id.words);
    Button voiceCmd = (Button) wordBar.findViewById(R.id.voiceword);
    LinearLayout ll1 = null;
    Button voiceCmd1 = null;
    //comment this block in the event of showing only one keyboard so that we can only
    //one autocorrect bar
    if (isLargeScreen) {
        ll1 = (LinearLayout) wordBar.findViewById(R.id.words1);
        voiceCmd1 = (Button) wordBar.findViewById(R.id.voiceword1);
    }

    voiceCmd.setOnClickListener(voiceClickListener);

    mCandidateView = new CandidateView(this);
    mCandidateView.setService(this);
    setCandidatesViewShown(true);
    mCandidateView.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    ll.addView(mCandidateView);             

    return wordBar;

}

【问题讨论】:

    标签: android android-softkeyboard


    【解决方案1】:

    我遇到了同样的问题。我在 Ced here 的帖子中找到了答案。

    解决办法是把这个添加到你的输入法服务中,

    @Override public void onComputeInsets(InputMethodService.Insets outInsets) {
        super.onComputeInsets(outInsets);
        if (!isFullscreenMode()) {
            outInsets.contentTopInsets = outInsets.visibleTopInsets;
        }
    }
    

    候选视图不会向上推动应用程序是有意的。来自doc

    请注意,由于候选视图倾向于显示和隐藏很多,它不会像软输入视图那样影响应用程序 UI:它永远不会导致应用程序窗口调整大小,只会导致它们被平移如果需要用户查看当前焦点。

    上面的 hack 增加了“内容”区域以包括候选视图区域。 onComputeInsets 的文档将帮助您理解这个概念。

    在您的 UI 中计算有趣的插图。默认实现使用候选帧的顶部作为可见插图,使用输入帧的顶部作为内容插图。

    【讨论】:

    • 谢谢!它已经有年头了,必须重新访问才能看到你的答案很有帮助:)
    • 这用于 Portait android 应用程序但在景观中无法使用此方法的任何建议g
    • @VishalAndroiddeveloper 你有什么解决办法吗?
    【解决方案2】:

    我遇到了同样的问题。问题是当我们添加CandidateView 时它不会调整用户界面。而不是调整它重叠在编辑文本上。

    像这样:

    简单的解决方案

    使用InputMethodService 继承的类有一个方法onComputeInsets。只需覆盖该功能并检查屏幕是否完全覆盖。如果屏幕不在fullScreenMode(); 中,则将此行添加到其中。它将从candidateView向上调整屏幕。

    outInsets.contentTopInsets = outInsets.visibleTopInsets;

    就这样

     @Override
    public void onComputeInsets(Insets outInsets) {
        super.onComputeInsets(outInsets);
        if (!isFullscreenMode()) {
            outInsets.contentTopInsets = outInsets.visibleTopInsets;
        }
    }
    

    结果将是

    【讨论】:

      猜你喜欢
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      相关资源
      最近更新 更多