After the Android activity is placed EditText, if there is no special adjustment, each time you enter the Activity,edittext will automatically get the focus, and then pop up the virtual keyboard, causing the picture to become crowded. Although Android is designed to reduce the number of users who click on the screen, it is not necessary to automatically eject the virtual keyboard in all cases, and the user may just want to see the data (such as a chat message).
Do not automatically eject the virtual keyboard when entering activity that contains edittext
Solve an activity to automatically eject the virtual keyboard
Just add the following code in the OnCreate phase of the activity.
GetWindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Or in Androidmanifest.xml, set the activity's Android:windowsoftinputmode attribute, as follows:
< Activity Android:name = "Org.magiclen.TestActivity" Android:label = "@string/app_name" Android:windowsoftinputmode = "Statehidden" />
After setting the Windowsoftinputmode property to state hidden, when entering activity, EditText will still get the focus, but will not immediately pop up the virtual keyboard, and then click EditText again to eject the keyboard. The problem has been solved successfully!
Solve a pop-up dialog and then automatically eject the virtual keyboard
If there is edittext in the dialog, you will also encounter the same activity, in the presence of dialog, and then automatically pop up the virtual keyboard status. The solution is the same as the activity, after obtaining the dialog window, use the Setsoftinputmode method to set the hidden keyboard, the code is as follows:
New Alertdialog.builder (this). Setview (EditText). Create ();d Ialog.getwindow (). Setsoftinputmode ( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);d ialog.show ();
Click on the blank space in Android to hide the soft keyboard
Inputmethodmanager imm=(Inputmethodmanager) Getsystemservice (Context.input_method_service); @Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method Stub if(event.getaction () = =Motionevent.action_down) {System.out.println ("Down"); if(Registeractivity. This. Getcurrentfocus ()! =NULL) { if(Registeractivity. This. Getcurrentfocus (). Getwindowtoken ()! =NULL) {Imm.hidesoftinputfromwindow (registeractivity. This. Getcurrentfocus (). Getwindowtoken (), inputmethodmanager.hide_not_always); } } } return Super. Ontouchevent (event); }
Android handles activity virtual keyboards containing edittext