Get the App
SLTechnology News&Howtos  ›  Development  › 

The solution to the problem of not popping up the focus of input method in Android EditText

Shulou Source: shulou.com Published: 2022-06-02 10:19:32 09月10日 Update

Android EditText does not pop up the solution to the focus problem of input method. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Look at the configuration of Activity in a manifest. If the page has EditText and we want to enter the page by default, we can set this sign: android:windowSoftInputMode=stateVisible, which will pop up the input method by default. Of course, there are other ways.

Summary of input method without popup in Android EditText

Method 1:

Which activity is selected in AndroidMainfest.xml, and set the windowSoftInputMode attribute to adjustUnspecified | stateHidden

For example:

< intent-filter>

< action android:name="android.intent.action.MAIN" />

< category android:name="android.intent.category.LAUNCHER" />

< /intent-filter>

< /activity>

Method 2:

To make EditText lose focus, use EditText's clearFocus method

For example:

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

Method 3:

Force hiding the Android input method window

For example:

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

2.EditText always does not eject the software keyboard

Example:

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

The problems of focus Focus and pop-up input method in android are studied. Read some examples on the Internet are not comprehensive enough, here a comprehensive summary.

One: why does EditText pop up the input method by default?

The same code, when encountered with EditText control interface, the machine will pop up the input method, some machines will not pop up. Sorry, I am also confused about this question, who knows can tell me, otherwise I will leave this problem behind, and make it clear when I study the android source code in the future. But. I have a solution.

Two: default pop-up and default off the input method solution.

1. Off by default, so as not to enter the Activity to open the input method, affecting the beauty of the interface.

① places an invisible LinearLayout in front of the EditText in the layout file, allowing him to take the lead in getting focus:

② method 2: first look at an attribute android:inputType: specify the type of input method, int type, you can use | Select multiple. For more information, please see android.text.InputType class. Values include: text,textUri

Phone,number, wait.

There is a phrase "If the given content type is TYPE_NULL then a soft keyboard will not be displayed for this text view" in Android SDK.

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

EditText.setOnTouchListener (new OnTouchListener () {public boolean onTouch (View v, MotionEvent event) {/ / TODO Auto-generated method stub int inType = editText.getInputType (); / / backup the input type editText.setInputType (InputType.TYPE_NULL); / / disable soft input editText.onTouchEvent (event) / / call native handler editText.setInputType (inType); / / restore input type return true;}})

two。 Pop up by default. Sometimes the default pop-up input method may be required as required. The plan is as follows:

EditText titleInput = (EditText) findViewById (R.id.create_edit_title); titleInput.setFocusable (true); titleInput.requestFocus (); onFocusChange (titleInput.isFocused ()); private void onFocusChange (boolean hasFocus) {final boolean isFocus = hasFocus; (new Handler ()) .postdelayed (new Runnable () {public void run () {InputMethodManager imm = (InputMethodManager) titleInput.getContext (). GetSystemService (Context.INPUT_METHOD_SERVICE); if (isFocus) {imm.toggleSoftInput (0, InputMethodManager.HIDE_NOT_ALWAYS)) } else {imm.hideSoftInputFromWindow (titleInput.getWindowToken (), 0);}, 100);}

I think that because the operation on UI is not valid in the main thread of Android, the pop-up input method must be implemented in Handler.

Three: personal understanding of focus and input method

To get the focus is to get the focus, and the bullet input method is the bullet input method. After gaining focus, the input method does not necessarily pop up. After searching the Internet, the mainstream answer is "it may not work if the interface is focus text, and it will take time to render UI."

Because I don't understand the source code, I don't have a comprehensive understanding of this. The focus and the input method can only be divided into two parts. The opening and closing of focus is very simple.

Acquisition of focus:

TitleInput.setFocusable (true); titleInput.requestFocus ()

Cancellation of focus:

TitleInput.setFocusable (false)

Four: the frequently called functions for dealing with soft keyboards are as follows:

1. Open the input method window:

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE); / / accept edit text or other views entered by the soft keyboard imm.showSoftInput (submitBt,InputMethodManager.SHOW_FORCED)

2. Close the window of access law.

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow (OpeListActivity.this.getCurrentFocus (). GetWindowToken (), InputMethodManager.HIDE_NOT_ALWAYS); / / accept edit text or other views entered by the soft keyboard inputMethodManagerwww.2cto.com .showSoftInput (submitBt,InputMethodManager.SHOW_FORCED)

3. Close the input method if it is open, open it if it is not open

InputMethodManager m = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE); m.toggleSoftInput (0, InputMethodManager.HIDE_NOT_ALWAYS)

4. Get the open status of the input method

InputMethodManager imm = (InputMethodManager) getSystemService (Context.INPUT_METHOD_SERVICE); boolean isOpen=imm.isActive ()

If isOpen returns true, it means the input method is open.

The solution to the problem that Android EditText does not pop up the focus of the input method is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Tags: Input input method focus method problem keyboard scheme interface that is properties text more machine source code type view solution page processing help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL Apple OPPO Reno Shulou Tech Info