【问题标题】:Android - Push up the actionbar when soft keyboard appearsAndroid - 出现软键盘时向上推操作栏
【发布时间】:2016-12-04 19:00:16
【问题描述】:

所以,我在我创建的自定义工具栏下方有一个 EditText。在edittext下,有一个listview。但是,当软键盘出现时,用户无法看到该列表,因为它被键盘挡住了。当出现软键盘时,如何将 EditText 移动到屏幕顶部将工具栏推出屏幕我正在使用 NoActionBar 主题任何帮助将不胜感激。

编辑:我已经试过了

android:windowSoftInputMode="adjustPan"

在 Manifests.xml 中

谢谢!

编辑: 当键盘出现时,我正在使用此代码来检测隐藏工具栏。

        numbersActivityRoot = (CoordinatorLayout) findViewById(R.id.numbers_activity_root_view);
        appBarLayout = (AppBarLayout) findViewById(R.id.appbarLayout);
        numbersActivityRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //r will be populated with the coordinates of your view that area still visible.
                numbersActivityRoot.getWindowVisibleDisplayFrame(r);

                int heightDiff = numbersActivityRoot.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    Log.v("NUMBERS","Hide toolbar");
                    appBarLayout.setExpanded(false);
                } else {
                    Log.v("NUMBERS","Show toolbar");
                    appBarLayout.setExpanded(true);
                }
            }
        }); 

虽然日志消息显示检测键盘的正确结果,但工具栏仍未隐藏。我做错了什么?

【问题讨论】:

  • 你试过android:windowSoftInputMode="adjustPan"吗?
  • 是的,我有。没有效果。
  • 请附上您的视图层次结构。
  • 好的。立即行动
  • 已添加。请检查。

标签: android android-layout android-toolbar


【解决方案1】:

如果EditText 在键盘启动时完全显示在屏幕上,adjustPan 将无济于事。就个人而言,我从不使用adjustPan,因为它会让系统以不可预测的方式移动您的窗口,而我根本不喜欢这样。

您需要解决两个主要问题:检测到键盘是否可见,以及将工具栏移出屏幕。

对于第一部分,框架没有提供直接通知键盘显示/隐藏的机制。在 StackOverflow 上有很多关于变通方法的帖子(如 this onethis one)。基本思想是将内容视图的高度与整个手机屏幕的高度进行比较,如果存在显着差异,您就假设软键盘占据了其他空间。

对于第二部分,我建议您使用AppBarLayout 的滚动/折叠功能,方法是为您的Toolbar 赋予属性layout_scrollFlags="scroll"(或者可能是"scroll|enterAlways")。当您使用第 1 部分中描述的技巧之一检测到键盘更改时,您可以使用 appBarLayout.setExpanded(boolean) 折叠或展开应用栏。

【讨论】:

  • 非常感谢您的回答。我认为这是隐藏工具栏的方法。但是,它仍然无法正常工作。你能再看看我最近的编辑,让我知道有什么问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
相关资源
最近更新 更多