Android實(shí)現(xiàn)鍵盤彈出界面上移的實(shí)現(xiàn)思路
1.首先說一下思路:
基本就是結(jié)合layout中ScrollView視圖和AndroidManifest.xml中activity中的android:windowSoftInputMode屬性配置實(shí)現(xiàn);
2.要了解android:windowSoftInputMode相應(yīng)的可以配置項(xiàng):
activity主窗口與軟鍵盤的交互模式,可以用來避免輸入法面板遮擋問題,Android1.5后的一個新特性。
這個屬性能影響兩件事情:
1.當(dāng)有焦點(diǎn)產(chǎn)生時,軟鍵盤是隱藏還是顯示
2.是否減少活動主窗口大小以便騰出空間放軟鍵盤
windowSoftInputMode的設(shè)置必須是下面列表中的一個值,或一個”state…”值加一個”adjust…”值的組合。在任一組設(shè)置多個值——多個”state…”values,例如&mdash有未定義的結(jié)果。各個值之間用|分開。
例如:<activity android:windowSoftInputMode="stateVisible|adjustResize". . . >
在這設(shè)置的值(除"stateUnspecified"和"adjustUnspecified"以外)將覆蓋在主題中設(shè)置的值
各值的含義:
stateUnspecified:軟鍵盤的狀態(tài)并沒有指定,系統(tǒng)將選擇一個合適的狀態(tài)或依賴于主題的設(shè)置
stateUnchanged:當(dāng)這個activity出現(xiàn)時,軟鍵盤將一直保持在上一個activity里的狀態(tài),無論是隱藏還是顯示
stateHidden:用戶選擇activity時,軟鍵盤總是被隱藏
stateAlwaysHidden:當(dāng)該Activity主窗口獲取焦點(diǎn)時,軟鍵盤也總是被隱藏的
stateVisible:軟鍵盤通常是可見的
stateAlwaysVisible:用戶選擇activity時,軟鍵盤總是顯示的狀態(tài)
adjustUnspecified:默認(rèn)設(shè)置,通常由系統(tǒng)自行決定是隱藏還是顯示
adjustResize:該Activity總是調(diào)整屏幕的大小以便留出軟鍵盤的空間
adjustPan:當(dāng)前窗口的內(nèi)容將自動移動以便當(dāng)前焦點(diǎn)從不被鍵盤覆蓋和用戶能總是看到輸入內(nèi)容的部分
例如:
AndroidManifest.xml文件中界面對應(yīng)的<activity>里加入
android:windowSoftInputMode="adjustPan" 鍵盤就會覆蓋屏幕
android:windowSoftInputMode="stateVisible|adjustResize" 屏幕整體上移(結(jié)合ScrollView實(shí)現(xiàn))
android:windowSoftInputMode="adjustPan|stateHidden" 軟鍵盤彈出,界面布局不變,這是解決彈出軟鍵盤,界面整體被壓縮的方式(會導(dǎo)致整個界面上移動,顯示效果不好)
3.具體實(shí)現(xiàn)
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等內(nèi)容;
3.2為AndroidManifest.xml文件中activity添加android:windowSoftInputMode="stateHidden|adjustResize"屬性
<activity android:name=".ui.AddAccountActivity" android:windowSoftInputMode="stateHidden|adjustResize" android:screenOrientation="landscape"/>
說明:
stateHidden:進(jìn)入Activity默認(rèn)隱藏鍵盤,通常需要看見整個頁面,用戶需要輸入時點(diǎn)擊輸入框;
adjustResize:界面調(diào)整大小,鍵盤留在底部,ScrollView內(nèi)容可以滾動,這樣就繼續(xù)可以看到整個頁面;
ScrollView通常不要設(shè)置android:fillViewport="true"
(作用就是布滿整個屏幕即使內(nèi)容高度沒有屏幕的高度)屬性(看實(shí)際需要吧),android:fillViewport="true"
導(dǎo)致界面無法滾動,API 19,21有這個問題,API 27沒有這個問題,主要看你適配的版本和需求了;
3.3效果圖如下
總結(jié)
以上所述是小編給大家介紹的Android實(shí)現(xiàn)鍵盤彈出界面上移,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
如何使用Flutter實(shí)現(xiàn)58同城中的加載動畫詳解
這篇文章主要給大家介紹了關(guān)于如何使用Flutter實(shí)現(xiàn)58同城中加載動畫詳?shù)南嚓P(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10android 震動和提示音的實(shí)現(xiàn)代碼
這篇文章主要介紹了android 震動和提示音的實(shí)現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12Android仿拉手網(wǎng)團(tuán)購App產(chǎn)品詳情界面效果
這篇文章主要介紹了Android仿拉手網(wǎng)團(tuán)購App產(chǎn)品詳情界面效果,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05EditText監(jiān)聽方法,實(shí)時的判斷輸入多少字符
在EditText提供了一個方法addTextChangedListener實(shí)現(xiàn)對輸入文本的監(jiān)控。本文分享了EditText監(jiān)聽方法案例,需要的朋友一起來看下吧2016-12-12Android應(yīng)用自動跳轉(zhuǎn)到應(yīng)用市場詳情頁面的方法
最近在工作中遇到一個需求,推廣部門要求實(shí)現(xiàn)應(yīng)用自動跳轉(zhuǎn)到應(yīng)用市場詳情頁面,通過查找一些資料,實(shí)現(xiàn)出來了,覺得有必要整理下方便以后或者有需要的朋友們參考借鑒,下面來一起詳細(xì)看看Android應(yīng)用自動跳轉(zhuǎn)到應(yīng)用市場詳情頁面的方法吧。2016-12-12