Android EditText Soft Keyboard some summary

Source: Internet
Author: User

See a manifest in the activity configuration, if this page has edittext, and we want to enter this page by default pop-up input method, you can set this zodiac: android:windowsoftinputmode= Statevisible, this will be the default pop-up input method, of course, there are other ways.

    1. <activity android:name=". Ui.login"
    2. android:configchanges="Orientation|keyboardhidden|locale"
    3. android:screenorientation="Portrait"
    4. Android:windowsoftinputmode="Statevisible|adjustpan" >
    5. </activity>

Android EditText Not Popup Input Method summary

Method One:

Which activity to select in Androidmainfest.xml, set Windowsoftinputmode property to Adjustunspecified|statehidden

For example:

    1. <activity android:name=". Main "
    2. Android:label="@string/app_name"
    3. Android:windowsoftinputmode="Adjustunspecified|statehidden"
    4. android:configchanges="Orientation|keyboardhidden" >
    5. < intent-filter>
    6. < action android:name="Android.intent.action.MAIN"/>
    7. < category android:name="Android.intent.category.LAUNCHER"/>
    8. </intent-filter>
    9. </activity>

Method Two:

Let EditText lose focus, use EditText's Clearfocus method

For example:

    1. EditText edit= (EditText) Findviewbyid (R.id.edit);
    2. Edit.clearfocus ();

Method Three:

Force Hide Android IME window

For example:

    1. EditText edit= (EditText) Findviewbyid (R.id.edit);
    2. Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
    3. Imm.hidesoftinputfromwindow (Edit.getwindowtoken (),0);

2.EditText always does not eject the software keyboard

Cases:

    1. EditText edit= (EditText) Findviewbyid (R.id.edit);
    2. Edit.setinputtype (Inputtype.type_null);

The problem of focus and popup input method in Android was studied. Some of the examples on the Internet are not comprehensive enough to summarize here.

A: Why does the edittext default pop-up input method?

The same code, touch the interface with the EditText control when some of the machine will pop-up input method, some of the machine will not pop up. Sorry, this question I also confused, who knows can tell me, otherwise I will leave this question, later study the Android source code to make a clear. But... I have a solution.

Two: Default popup and default turn off the IME solution.

1. Default off, do not enter the activity to open the input method, the impact of beautiful interface.

① in the layout file, place an invisible linearlayout in front of the edittext, allowing him to take the lead in getting the focus:

    1. <linearlayout
    2. android:focusable="true"
    3. Android:focusableintouchmode="true"
    4. android:layout_width="0px"
    5. android:layout_height="0px"/>

② method Two: First look at a property Android:inputtype: Specify the type of input method, int type, you can select more than one. The value can be referenced by: Android.text.InputType class. The values include: Text,texturi,

Phone,number, et.

In the Android SDK there is such a phrase "If the given content type is Type_null then a soft keyboard would not be displayed for this text view",

First change the EditText inputtype to Type_null, the input method will not pop up. Then set the listener, and then reset its inputtype.

  1. Edittext.setontouchlistener (new Ontouchlistener () {
  2. Public Boolean OnTouch (View V, motionevent event) {
  3. //TODO auto-generated method stub
  4. int intype = Edittext.getinputtype (); //backup the input type
  5. Edittext.setinputtype (Inputtype.type_null); //Disable soft input
  6. Edittext.ontouchevent (event); //Call native handler
  7. Edittext.setinputtype (Intype); //Restore input type
  8. return true;
  9. }
  10. });

2. Default Popup. Sometimes, you may require the default popup input method as required. The scheme is as follows:

  1. EditText titleinput = (EditText) Findviewbyid (r.id.create_edit_title);
  2. Titleinput.setfocusable (true);
  3. Titleinput.requestfocus ();
  4. Onfocuschange (titleinput.isfocused ());
  5. Private void Onfocuschange (boolean hasfocus)
  6. {
  7. Final Boolean isfocus = Hasfocus;
  8. (new Handler ()). postdelayed (new Runnable () {
  9. Public void Run () {
  10. Inputmethodmanager IMM = (Inputmethodmanager)
  11. Titleinput.getcontext (). Getsystemservice (Context.input_method_service);
  12. if (Isfocus)
  13. {
  14. Imm.togglesoftinput (0, inputmethodmanager.hide_not_always);
  15. }
  16. Else
  17. {
  18. Imm.hidesoftinputfromwindow (Titleinput.getwindowtoken (),0);
  19. }
  20. }
  21. }, 100);
  22. }

I think that because the operation of the UI is invalid in the main thread of Android, it is necessary to implement the popup input method in handler.

Three: Personal understanding of focus and Input method

Get focus is to get focus, bouncing input method is bouncing input method. Get focus after not sure will pop-up input method, in the online search a circle, the mainstream answer is "There is open interface is the text of the focus is also not possible, UI rendering is required time" ...

Because I do not understand the source code, I do not have a comprehensive understanding of this point. Only focus and input methods can be divided into two blocks to handle. Focus opening and closing is particularly straightforward.

Acquisition of Focus:

    1. Titleinput.setfocusable (true);
    2. Titleinput.requestfocus ();

Cancellation of Focus:

    1. Titleinput.setfocusable (false);

Four: The functions of handling soft keyboards that are often called are as follows:

1. Open the Input Method window:

    1. Inputmethodmanager Inputmethodmanager = (inputmethodmanager) getsystemservice (Context.input_method_service);
    2. Edit text or other views that accept soft keyboard input
    3. Imm.showsoftinput (submitbt,inputmethodmanager.show_forced);

2. Close the Access Law window

    1. Inputmethodmanager Inputmethodmanager = (inputmethodmanager) getsystemservice (Context.input_method_service);
    2. Inputmethodmanager.hidesoftinputfromwindow (opelistactivity. This.getcurrentfocus (). Getwindowtoken (),
    3. Inputmethodmanager.hide_not_always);
    4. Edit text or other views that accept soft keyboard input
    5. Inputmethodmanagerwww.2cto.com
    6. . Showsoftinput (submitbt,inputmethodmanager.show_forced);

3, if the input method is turned off, if not open then open

    1. Inputmethodmanager m= (Inputmethodmanager) Getsystemservice (Context.input_method_service);
    2. M.togglesoftinput (0, inputmethodmanager.hide_not_always);

4, get the input method open state

    1. Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);
    2. Boolean isopen=imm.isactive ();

IsOpen returns true to indicate that the input method is open

Original address: http://mobile.51cto.com/aprogram-403138.htm

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.