Working with the Soft Keyboard | CodePath Android Cliffnotes

文章推薦指數: 80 %
投票人數:10人

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: orinJava: myEditText.setImeOptions(EditorInfo.IME_ACTION_DONE); myEditText.setImeOptions(EditorInfo.IME_ACTION_DONE) SeetheEditTextdocumentationforamoredetailedlookatimeOptions. ConfiguringtheSoftKeyboardMode ThesoftkeyboardcanbeconfiguredforeachactivitywithintheAndroidManifest.xmlfileusingtheandroid:windowSoftInputModeattributetoadjustbothdefaultvisibilityandalsohowthekeyboardaffectstheUIwhendisplayed. ShowingtheKeyboardwhenActivityStarts AlthoughAndroidgivesfocustothefirsttextfieldinyourlayoutwhentheactivitystarts,itdoesnotshowthesoftkeyboard.Toshowthekeyboardwhenyouractivitystarts,addtheandroid:windowSoftInputModeattributetotheelementwiththe"stateVisible"valuewithintheAndroidmanifest.Checkoutthisguideformoredetails.WithintheAndroidManifest.xmlfile: Theoptionsforthemodeincludetwoaspects:visibilityofthekeyboardandadjustmentoftheUI.VisibilityoptionsincludestateUnchanged,stateHidden,stateVisibleandseveralotherslistedhere. ChangingUIReaction Thevirtualkeyboardreducestheamountofspaceavailableforyourapp'sUI.Wecanalsousethissameandroid:windowSoftInputModepropertywithinthenodetochangethewaythatthesoftkeyboarddisplaystheviewelementswhenappearingwithintheAndroidManifest.xmlfile: Theoptionsforthemodeincludetwoaspects:visibilityandadjustment.AdjustmentoptionsincludeadjustResize,adjustPan,andadjustUnspecifiedandarelistedinfullhere.Bothvisibilityandadjustmentcanbecombinedwith: Seetheguideonkeyboardvisibilityformoredetails. Troubleshooting ToolbarHeightExpandsonUIResize ToavoidincorrectToolbarheightcalculations,youcanaddandroid:fitsSystemWindows="true"(learnmore)totheparentlayoutoftheToolbar.Inmanycases,thisshouldresolvetheissue. References http://developer.android.com/training/keyboard-input/style.html http://developer.android.com/training/keyboard-input/navigation.html http://developer.android.com/training/keyboard-input/commands.html http://developer.android.com/training/keyboard-input/visibility.html



請為這篇文章評分?