2016-12-04 93 views
0

因此,我在创建的自定义工具栏下方有一个EditText。在edittext中,有一个listview。但是,当软键盘出现时,用户看不到列表,因为它被键盘阻挡。如何将EditText移动到屏幕顶部当软键盘出现时将工具栏推出屏幕我正在使用NoActionBar主题任何帮助将不胜感激。Android - 当软键盘出现时向上推动操作栏

编辑:我已经尝试

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); 
       } 
      } 
     }); 

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

+0

你试过android:windowSoftInputMode =“adjustPan”吗? –

+0

是的,我有。没有效果。 – Asym

+0

请附上您的视图层次结构。 –

回答

2

adjustPan如果在键盘启动时EditText完全显示在屏幕上,则不起作用。就我个人而言,我从来没有使用adjustPan,因为它可以让系统移动你的窗口不可预知的数量,我只是不喜欢这一点。

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

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

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

+0

非常感谢您的回答。我认为这是隐藏工具栏的方法。但是,它仍然无法正常工作。你能再看看我最近的编辑,让我知道什么是错的? – Asym