2016-12-04 54 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