1.首先說一下思路:
基本就是結合layout中ScrollView視圖和AndroidManifest.xml中activity中的android:windowSoftInputMode屬性配置實現;
2.要了解android:windowSoftInputMode相應的可以配置項:
activity主窗口與軟鍵盤的交互模式,可以用來避免輸入法面板遮擋問題,Android1.5后的一個新特性。
這個屬性能影響兩件事情:
1.當有焦點產生時,軟鍵盤是隱藏還是顯示
2.是否減少活動主窗口大小以便騰出空間放軟鍵盤
windowSoftInputMode的設置必須是下面列表中的一個值,或一個”state…”值加一個”adjust…”值的組合。在任一組設置多個值――多個”state…”values,例如&mdash有未定義的結果。各個值之間用|分開。
例如:<activity android:windowSoftInputMode="stateVisible|adjustResize". . . >
在這設置的值(除"stateUnspecified"和"adjustUnspecified"以外)將覆蓋在主題中設置的值
各值的含義:
stateUnspecified:軟鍵盤的狀態并沒有指定,系統將選擇一個合適的狀態或依賴于主題的設置
stateUnchanged:當這個activity出現時,軟鍵盤將一直保持在上一個activity里的狀態,無論是隱藏還是顯示
stateHidden:用戶選擇activity時,軟鍵盤總是被隱藏
stateAlwaysHidden:當該Activity主窗口獲取焦點時,軟鍵盤也總是被隱藏的
stateVisible:軟鍵盤通常是可見的
stateAlwaysVisible:用戶選擇activity時,軟鍵盤總是顯示的狀態
adjustUnspecified:默認設置,通常由系統自行決定是隱藏還是顯示
adjustResize:該Activity總是調整屏幕的大小以便留出軟鍵盤的空間
adjustPan:當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分
例如:
AndroidManifest.xml文件中界面對應的<activity>里加入
android:windowSoftInputMode="adjustPan" 鍵盤就會覆蓋屏幕
android:windowSoftInputMode="stateVisible|adjustResize" 屏幕整體上移(結合ScrollView實現)
android:windowSoftInputMode="adjustPan|stateHidden" 軟鍵盤彈出,界面布局不變,這是解決彈出軟鍵盤,界面整體被壓縮的方式(會導致整個界面上移動,顯示效果不好)
3.具體實現
3.1定義ScrollView
<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:scrollbars="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/set_account_info" android:layout_marginTop="@dimen/business_management_title_top" style="@style/large_size_home_main_color_style" android:layout_gravity="center_horizontal"/> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/add_confirm_top" android:focusable="true" android:focusableInTouchMode="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add_accout_bank_title" style="@style/cheque_collection_hint" android:layout_alignBaseline="@+id/accout_bank" android:text="@string/accout_bank"/> <EditText android:layout_width="@dimen/add_account_code_w" android:layout_marginTop="@dimen/common_gap" android:layout_toRightOf="@+id/add_accout_bank_title" style="@style/cheque_collection_et" android:id="@+id/accout_bank"/> </RelativeLayout> <Button android:id="@+id/confirm_add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/common_btn_style" android:layout_gravity="center_horizontal" android:layout_marginTop="@dimen/add_confirm_top" android:text="@string/confirmed" /> </LinearLayout></ScrollView>
即ScrollVIew中包裹EditText等內容;
3.2為AndroidManifest.xml文件中activity添加android:windowSoftInputMode="stateHidden|adjustResize"屬性
<activity android:name=".ui.AddAccountActivity" android:windowSoftInputMode="stateHidden|adjustResize" android:screenOrientation="landscape"/>
說明:
stateHidden:進入Activity默認隱藏鍵盤,通常需要看見整個頁面,用戶需要輸入時點擊輸入框;
adjustResize:界面調整大小,鍵盤留在底部,ScrollView內容可以滾動,這樣就繼續可以看到整個頁面;
ScrollView通常不要設置android:fillViewport="true"(作用就是布滿整個屏幕即使內容高度沒有屏幕的高度)屬性(看實際需要吧),android:fillViewport="true"導致界面無法滾動,API 19,21有這個問題,API 27沒有這個問題,主要看你適配的版本和需求了;
3.3效果圖如下

總結
以上所述是小編給大家介紹的Android實現鍵盤彈出界面上移,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答