在開發中經常會遇到這樣的問題,進入一個含Edittext的Activity後總是會自動彈出軟件盤。但是有時候我們又不想讓它彈出,因爲影響美觀,而且用戶可能也會反感。最好的做法是剛進入時不讓彈出,在用戶輸入的時候才讓彈出,把主動權交個用戶是對用戶最大的尊重。
廢話這麼多,直接上代碼
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden"/>
發現用stateAlwaysHidden也可以禁止彈出軟鍵盤
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
stateHidden和stateAlwaysHidden的區別
既然stateHidden和stateAlwaysHidden都可以實現禁止彈出軟鍵盤,那這兩者到底有什麼區別呢?網上看很多人都說得不清不楚,還不如自己上Android官網看最權威
"stateHidden " |
The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity. |
"stateAlwaysHidden " |
The soft keyboard is always hidden when the activity's main window has input focus. |
發現官網的說明還是有點晦澀難懂的,我以我的理解說明下
stateHidden:當用戶進入Activity後保證是隱藏軟鍵盤的,但是如果是從另一個頁面返回該頁面就不能保證了。比如說用戶進入A頁面,然後在輸入框輸入內容,未收起軟鍵盤直接進入B頁面,再從B頁面返回A頁面,此時A頁面中軟鍵盤還是顯示的。又或者是用戶由A頁面進入B頁面,在B頁面彈出軟鍵盤,未收起直接返回A頁面,此時A頁面中軟鍵盤還是顯示的。
stateAlwaysHidden:當用戶剛進入Activity後軟鍵盤也是隱藏的。與stateHidden的區別是,還是以上面的例子來說明,比如說用戶進入A頁面,然後在輸入框輸入內容,未收起軟鍵盤直接進入B頁面,再從B頁面返回A頁面,此時A頁面中軟鍵盤是收起來的。又或者是用戶由A頁面進入B頁面,在B頁面彈出軟鍵盤,未收起直接返回A頁面,此時A頁面中軟鍵盤是收起來的。