Working with the Soft Keyboard | CodePath Android Cliffnotes
文章推薦指數: 80 %
The Android system shows an on-screen keyboard, known as a soft input method, when a text field in your UI receives focus. To provide the best user ...
JumptoSectionTableofContentsDisplayingtheSoftKeyboardAVDManagerShowingSoftKeyboardProgrammaticallyHidingtheSoftKeyboardProgrammaticallyAddinga"Done"KeyConfiguringtheSoftKeyboardModeShowingtheKeyboardwhenActivityStartsChangingUIReactionTroubleshootingToolbarHeightExpandsonUIResizeReferencesWorkingwiththeSoftKeyboardEditPagePageHistoryTheAndroidsystemshowsanon-screenkeyboard,knownasasoftinputmethod,whenatextfieldinyourUIreceivesfocus.Toprovidethebestuserexperience,youcanspecifycharacteristicsaboutthetypeofinputyouexpect(suchaswhetherit'saphonenumberoremailaddress)andhowtheinputmethodshouldbehave(suchaswhetheritperformsauto-correctforspellingmistakes).
DisplayingtheSoftKeyboard
AVDManager
Bydefault,thesoftkeyboardmaynotappearontheemulator.Ifyouwanttotestwiththesoftkeyboard,besuretoopenuptheAndroidVirtualDeviceManager(Tools=>Android=>AVDManager)anduncheck"EnableKeyboardInput"foryouremulator.
Nowrestarttheemulator.Seethesescreenshotsforavisualreference.
ShowingSoftKeyboardProgrammatically
Thefollowingcodewillrevealthesoftkeyboardfocusedonaspecifiedview:
publicvoidshowSoftKeyboard(Viewview){
if(view.requestFocus()){
InputMethodManagerimm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
}
}
funshowSoftKeyboard(view:View){
if(view.requestFocus()){
valimm:InputMethodManager=
getSystemService(Context.INPUT_METHOD_SERVICE)asInputMethodManager
imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT)
}
}
HidingtheSoftKeyboardProgrammatically
YoucanforceAndroidtohidethevirtualkeyboardusingtheInputMethodManager,callinghideSoftInputFromWindow,passinginthetokenofthewindowcontainingyoureditfield.
publicvoidhideSoftKeyboard(Viewview){
InputMethodManagerimm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
funhideSoftKeyboard(view:View){
valimm=
getSystemService(Context.INPUT_METHOD_SERVICE)asInputMethodManager
imm.hideSoftInputFromWindow(view.windowToken,0)
}
Thiswillforcethekeyboardtobehiddeninallsituations.
Addinga"Done"Key
Inthekeyboard,youcanhidethe"Next"keyandadd"Done"insteadbyaddingthefollowingtotheimeOptionsfortheEditTextview:
延伸文章資訊
- 1Fix problems with Gboard - Android - Google Help
On your Android phone or tablet, open the Settings app Settings app . · Tap System and then Langu...
- 2Android read from keyboard - Stack Overflow
I can't have any button or textfields, so I have to read keyboard input without listening to a Ed...
- 3Keymaps and Keyboard Input | Android Open Source
This document describes how keyboard input gets translated into Android actions and how you can c...
- 4Handle input method visibility | Android Developers
When input focus moves into or out of an editable text field, Android shows or hides the input me...
- 5Slide your finger to type - Android - Gboard Help - Google ...
Languages & input. Tap Virtual keyboard and then Gboard. Pick an option, like Glide typing or Voi...