The android soft keyboard management

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/generallizhong/article/details/100282321

We need to manage the software, such as automatically pop up in many cases, autofocus, hides, etc., said here about the state of the soft keyboard to hide or display.

First, the automatic pop-up soft keyboard

Timer timer=new Timer();  
timer.schedule(new TimerTask() {  
   public void run() {  
      InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
      inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
                     }  
        }, 2000);  

Second, the soft keyboard

Activity Set:

Android:windowSoftInputMode="stateUnspecified",

Default setting: soft keyboard status (hidden or visible) is not specified. The system will choose an appropriate state or set dependent on the subject matter.                                       

 "StateUnchanged", the soft keyboard is held the last state.

 "StateHidden", when the user selects the Activity, the soft keyboard is hidden.                                       

 "StateAlwaysHidden", the soft keyboard is always hidden.

 "StateVisible" ,. soft keyboard is visible.                                        

"StateAlwaysVisible", when the user selects this Activity, the soft keyboard is visible.

 "AdjustUnspecified",. It is not specified whether the Activity resize the main window to allow space soft keyboard, or whether the contents of the window obtain the current focus on the screen is visible. The system will automatically select these modes depends on whether the content is a major window has any layout views can scroll their contents. If there is such a view, the window is resized, the assumption can scroll the window contents in a smaller area visible. The main window is the default setting behavior. In other words, the system automatically decide whether to adopt the translational mode or compressed mode, the determining factor is whether the content can be scrolled.                                       

 "AdjustResize", (compressed mode) when the soft keyboard pops up on the screen to adjust the size of the main window to make room soft keyboard. 

"AdjustPan"] (Pan Mode: When the input frame is not blocked, the model does not adjust the layout, but when the input box to be obscured, the window will pan that is, the input mode is always maintained. box is visible...

Third, the soft keyboard:

EditText edit=(EditText)findViewById(R.id.edit);  
           InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
           imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

Four, EditText default software keyboard does not pop up

method one:

Choose which activity in AndroidMainfest.xml, set windowSoftInputMode property adjustUnspecified | stateHidden

E.g:

<activity Android:name=".Main"
                  Android:label="@string/app_name"
                  Android:windowSoftInputMode="adjustUnspecified|stateHidden"
                  Android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action Android:name="android.intent.action.MAIN" />
                <category Android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

 

Method Two:

Let EditText loses focus, the use of clearFocus method EditText

E.g:

EditText edit=(EditText)findViewById(R.id.edit);
           edit.clearFocus();

Method three:

Forced to hide Android input method window

E.g:

EditText edit=(EditText)findViewById(R.id.edit);  
           InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
           imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

Five, EditText not always pop up software keyboard

Example:

EditText edit=(EditText)findViewById(R.id.edit);
       edit.setInputType(InputType.TYPE_NULL);

Several relatively simple management.

Guess you like

Origin blog.csdn.net/generallizhong/article/details/100282321