簡體   English   中英

Android-出現軟鍵盤時按下操作欄

[英]Android - Push up the actionbar when soft keyboard appears

因此,我在創建的自定義工具欄下面有一個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);
                }
            }
        }); 

盡管日志消息顯示了用於檢測鍵盤的正確結果,但是工具欄仍然沒有隱藏。 我究竟做錯了什么?

如果鍵盤抬起時EditText完全顯示在屏幕上,則adjustPan無效。 就我個人而言,我從不使用adjustPan因為它可以使系統將窗口移動到無法預測的程度,而我根本不喜歡這樣。

您需要解決兩個主要問題:檢測鍵盤可見,以及將工具欄移出屏幕。

對於第一部分,框架沒有提供任何機制來直接告知正在顯示/隱藏鍵盤。 在StackOverflow上有很多文章都提供了解決方法(例如這一節這一節 )。 基本思想是將內容視圖的高度與整個手機屏幕的高度進行比較,如果兩者之間存在顯着差異,則可以假設軟鍵盤正在占用其他空間。

對於第二部分,我建議您通過為Toolbar賦予屬性layout_scrollFlags="scroll" (或也許是"scroll|enterAlways" )來使用AppBarLayout的滾動/折疊功能。 當您使用第1部分中描述的技巧之一檢測到鍵盤更改時,可以使用appBarLayout.setExpanded(boolean)折疊或展開應用程序欄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM