IME in Android

Source: Internet
Author: User

Introduced input method I thought of edittext, the input method can be automatically based on the InputType to change the layout of the keyboard, in the payment wallet is also particularly hidden in the system's own input method, directly let users use the software's own input method, improve security. Therefore, we should have some understanding of the input method, let the input method for their own use.

First, get the input management object

Final Inputmethodmanager Inputmethodmanager = (inputmethodmanager) getsystemservice (Context.input_method_service);

Second, the operation input method

1. Force display Input Method

Through testing, I found that the mandatory display input method must be used a edittext to do, in fact, quite normal, the input method itself is to edittext input, so naturally to define a edittext.

I used a edittext and two buttons in the layout file.

In Java code, we want to find this edittext, and then do the following:

Inputmethodmanager.showsoftinput (EditText, inputmethodmanager.show_forced);

All code:

 final  EditText EditText = (EditText) Findvi                Ewbyid (R.id.edittext);  final  inputmethodmanager Inputmethodmanager =         = new    Onclicklistener () {@Override  public  void   OnClick (View v) {   

2. Force hidden Input Method

With the layout file or just that, the hidden input method will not need to edittext. Just write the following code directly in the button's Click event.

Inputmethodmanager.hidesoftinputfromwindow (V.getwindowtoken (), 0);

All code:

Button hidebtn = (Button) Findviewbyid (R.id.hide_button);        Hidebtn.setonclicklistener (new  Onclicklistener () {                        @Override            public  void  OnClick (View v) {                ///  TODO Auto-generated method stub                Inputmethodmanager.hidesoftinputfromwindow (V.getwindowtoken (), 0);            }        );

3. Show/Hide input methods according to Input method status

Sometimes we need to deal with the state of the input method: If the input method is already displayed, then hide it, and if the IME is not displayed now, then show it.

Inputmethodmanager.togglesoftinput (0, inputmethodmanager.hide_not_always);

All code:

        Btn.setonclicklistener (new  Onclicklistener () {            @Override            publicvoid OnClick (View v) {                inputmethodmanager.togglesoftinput (0, inputmethodmanager.hide_not_always);            }        });

4. Do not eject the keyboard automatically

With EditText control, the first display will automatically get focus, and pop up the keyboard, if you do not want to pop-up input method, you can write this line of code in the activity's oncreat

// Disable Auto popup input box         This . GetWindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

// This.getwindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // Use this to prevent the automatic pop-up of the input method fragment.

You can also set the corresponding activity in the Mainfest file.

Android:windowsoftinputmode= "Statehidden" or android:windowsoftinputmode= "stateunchanged".

<ActivityAndroid:name=". Main "Android:label= "@string/app_name"Android:windowsoftinputmode= "adjustunspecified| Statehidden"android:configchanges= "Orientation|keyboardhidden">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>

Three, Input Method constants

public static final int hide_implicit_only

A flag in Hidesoftinputfromwindow (ibinder, int) that indicates that the window is hidden if the user does not explicitly display the soft-keyboard window.

public static final int hide_not_always

A flag in Hidesoftinputfromwindow (ibinder, int) that indicates that the Soft keyboard window is always hidden, unless it starts with a show_forced display.

public static final int Result_hidden

Showsoftinput (View, int, resultreceiver) and Hidesoftinputfromwindow (ibinder, int, resultreceiver) Resultreceiver result code flag: The soft keyboard window transitions from display to hidden state.

public static final int Result_shown

Showsoftinput (View, int, resultreceiver) and Hidesoftinputfromwindow (ibinder, int, resultreceiver) Resultreceiver result code flag: Soft Keyboard window switches from hidden to displayed state.

public static final int Result_unchanged_hidden

Showsoftinput (View, int, resultreceiver) and Hidesoftinputfromwindow (ibinder, int, resultreceiver) Resultreceiver result code flag: The soft keyboard window remains hidden when it is not changed.

public static final int Result_unchanged_shown

Showsoftinput (View, int, resultreceiver) and Hidesoftinputfromwindow (ibinder, int, resultreceiver) Resultreceiver result code flag: The soft keyboard window remains in the state when it is displayed.

public static final int show_forced

The Showsoftinput (View, int) flag, which means that the user forces the input method to open (such as long press the menu key) and remains open until it is explicitly closed.

public static final int show_implicit

The Showsoftinput (View, int) flag, which indicates that an input window is implicitly displayed, is not directly required by the user. The window may not be displayed.

Four, dynamic change input keyboard layout

We know that EditText can set the type of input so that it can directly control the keyboard layout of the input method. But what if we want to dynamically change the type of input in the code?

// the input box that sets the user name is a usernameet.setinputtype that can enter numbers and letters (Inputtype.type_class_text | Inputtype.type_text_variation_normal);

Usernameet is a edittext, the method of setting the input type through code is Setinputtype, and the different input types can be set after passing in different constants. This is the knowledge in EditText, anyway, and the input method is related, so incidentally mention.

V. Input method and Activity view relationship

The input method is special, it is a view drawn in Phonewindow, you can think of it as a dialog, so it naturally has its own position. And its position is assigned by the activity, below to analyze its layout.

1. When the activity was initially laid out

I put a textview up and down the activity to represent the top and end of ActivityView. The effect of running out is as follows:

2. Android:windowsoftinputmode= "Stateunspecified"

When we add this phrase to the activity definition, the input method is shown in the default mode, the state of the soft keyboard (hidden or visible) is not specified. The system will select an appropriate state or a theme-dependent setting.

< Activity             Android:name =". Mainactivity "            android:label=" @string/app_name "              android: Windowsoftinputmode= "stateunspecified">

When we click Show Input, we jump out of the input method

Effect:

You can see that the input method is directly covered by the original activity, the following bottom directly covered. If you want to see the text that is being covered, you have to set a scrollview for the outer layer of the layout.

3. Android:windowsoftinputmode= "Statevisible"

In this mode, activity and input method are at the same time, that is, the activity starts after the input method will also start, do not need us to click Show Input. The effect is the same as above, the input method covers the following text.

4. Android:windowsoftinputmode= "Statealwaysvisible"

The same as above, but the explanation is that when the user chooses this activity, the keyboard is visible. My understanding is that if the activity gets the focus, the keyboard will automatically appear. There is no difference between the feeling and the above.

5. Android:windowsoftinputmode= "Statehidden"

The input method is not displayed when there is edittext in activity, that is, the input method will only appear when you click EditText. The effect is to cover the bottom text below.

6. Android:windowsoftinputmode= "Statealwayshidden"

This is the same as above, also does not automatically display the input method. Effect and also cover the text below.

7. Android:windowsoftinputmode= "Adjustresize"

Compression mode. When the input method appears, the activity re-adjusts the layout of the interface, so that the original interface and input methods in the same plane. The effect is as follows, it is obvious to see that bottom is not pressed.

8.android:windowsoftinputmode= "Adjustpan"

panning mode. when the input box is not obscured, the layout is not adjusted, but when the input box is obscured, the window pans. That is, the pattern is always to keep the input box visible

Reference from:

http://my.oschina.net/jbcao/blog/61035

Http://www.cnblogs.com/weixing/p/3300908.html

http://blog.csdn.net/blueangle17/article/details/12753397

IME in Android

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.