【问题标题】:android - show keyboard fix bottom framelayoutandroid - 显示键盘修复底部框架布局
【发布时间】:2015-09-24 16:38:41
【问题描述】:

我正在开发一个 android 应用程序,其中我的 mainActivity 在底部有 5 个选项卡,以 FrameLayout 表示。每个选项卡都指向一个特定的片段,该片段根据单击的选项卡加载。

在我的第一个选项卡中,我有一个 EditText。当编辑文本被聚焦时,我需要通过“向上”移动('EditText and Send button)的布局来显示键盘,而无需向上推动包括底部选项卡框架布局在内的其他组件。同样,隐藏键盘时,“EditText and Send btn”布局应位于选项卡框架布局的正上方。有人可以建议我解决这个问题吗?

(假设情况与 Facebook 评论中的情况相同。当我们进入帖子进行评论时,布局('Write Comment' ET 和 POST btn)将被向上推,而不会移动任何其他组件,包括底部选项卡(' News Feed', 'Requests', 'Messenger', 'Notifications', 'More'))

我不想让框架布局 Visibility.Gone/Visible 因为当键盘向上/向下滑动时它会以丑陋的动画结束。

【问题讨论】:

    标签: android android-activity keyboard fragment show-hide


    【解决方案1】:

    使用没有 weightSum 的 LinearLayout,并为布局中的主视图提供 0dp 高度和重量 1

    【讨论】:

    • 我听不懂你想说什么。你能详细说明你的答案吗?
    【解决方案2】:

    我有完全相同的问题。我能找到的最佳解决方法(如原始问题中所述)如下。但这是一个边缘解决方案,而不是一个很好的解决方案。

    设置weightSum的问题是,如果包含编辑框的scrollview较长,按钮会消失。最好的解决方案是让按钮在正常视图中停留在页面底部,但在屏幕键盘显示时隐藏。

     public boolean dispatchTouchEvent(MotionEvent event) {
    
        View v = getCurrentFocus();
        boolean ret = super.dispatchTouchEvent(event);
    
        if (v instanceof EditText) {
            View w = getCurrentFocus();
            int scrcoords[] = new int[2];
            w.getLocationOnScreen(scrcoords);
            float x = event.getRawX() + w.getLeft() - scrcoords[0];
            float y = event.getRawY() + w.getTop() - scrcoords[1];
            if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
                layButton.setVisibility(View.VISIBLE);
            }
            else
            {
                layButton.setVisibility(View.GONE);
            }
        }
        return ret;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 2017-09-01
      • 2018-10-31
      • 2020-12-03
      • 2018-12-01
      • 2020-03-14
      相关资源
      最近更新 更多