Call the soft keyboard in Android

Source: Internet
Author: User

When we click in the EditText provided by Android, we will automatically eject the soft keyboard, in fact, the control of the soft keyboard can be achieved by Inputmethodmanager this class. The way we need to control the soft keyboard is that two kinds of one are like edittext when the OnClick event occurs when a soft keyboard appears, and when a program is opened automatically pop-up soft keyboard.

 Public classInputmethodmanagertest extends Activity implements onclicklistener{Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate); LinearLayout Layout=NewLinearLayout ( This); Linearlayout.layoutparams Layoutparams=NewLinearlayout.layoutparams (linearlayout.layoutparams.wrap_content,linearlayout.layoutparams.wrap_content); Button=NewButton ( This); Button.setid (123); Button.settext ("Hello Gaomatrix"); Button.setonclicklistener ( This);          Layout.addview (button, layoutparams);             Setcontentview (layout); /** Use a timer to control when you open the activity, a soft keyboard appears*/Timer Timer=NewTimer (); Timer.schedule (NewTimerTask () {@Override Public voidrun () {Inputmethodmanager Inputmethodmanager=(Inputmethodmanager) Getsystemservice (Context.input_method_service); Inputmethodmanager.togglesoftinput (0, inputmethodmanager.hide_not_always); }          },  -); }      /** * Trigger display soft keyboard when clicking Event*/@Override Public voidOnClick (View v) {Inputmethodmanager Inputmethodmanager=(Inputmethodmanager) Getsystemservice (Context.input_method_service); Inputmethodmanager.togglesoftinput (0, inputmethodmanager.hide_not_always); }  

The explanation in the API for the Togglesoftinput method inside this Inputmethodmanager class is: This method toggles the Input Method window display. If the input window is already displayed, it gets hidden. If not the input window would be displayed. This method in the interface to switch the function of the input method, if the input method out of the real state, he will hide, if in a hidden state, display input method. For the second way into the activity, the soft keyboard is automatically displayed, in a timer, that is, executed in a thread, but only for a delay of 2 seconds, because the Android program in the OnCreate function does not complete the screen drawing.

Of course we can also control the display and hiding of the soft keyboard separately

// Show          Inputmethodmanager IMM = (inputmethodmanager)this. Getsystemservice (Context.input_method_service);           0 );                // Hidden          0

The view in the code above is the view of the event when the button is clicked, or the layout of the activity as a whole (the test of the idiot can run normally)

There are two other keyboard controls that are often used:

1. Edittext.setimeoptions (EDITORINFO.IME_FLAG_NO_EXTRACT_UI); When you click EditText, a full screen input window will not pop up

2.edittext.setinputtype (Inputtype.type_null); When you click EditText, the soft keyboard is shielded

Call the soft keyboard in Android

Related Article

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.