Android handles activity virtual keyboards containing edittext

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.