EditText Focus Issues in Android

Source: Internet
Author: User
Tags gety

Https://www.jianshu.com/p/3d31d681f4bc

Problem: Do content verification when EditText loses focus

Scenario: User edits EditText are emptied, the soft keyboard is turned off when a blank space is clicked, and the contents of the checksum are EditText set to "1" if empty.
The problem is far from easy to see.

First step: Add focus monitoring
view.setOnFocusChangeListener(new OnFocusChangeListener() {            @Override            public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus){ Log.d("Tag","lose focus"); if(TextUtils.isEmpty(((EditText)v).getText().toString().trim())){ view.setText("1"); } }else { Log.d("Tag","get focus"); } } });
Step two: Turn off the soft keyboard when you click on a blank Space

Use the following code in the activity:

 @OverridePublicBooleanDispatchtouchevent(Motionevent ev) {if (ev.getaction () = = Motionevent.action_down) {View v = getcurrentfocus ();if (Isshouldhideinput (v, Ev)) {Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (context.input_method_ SERVICE);if (IMM! =NULL) {Imm.hidesoftinputfromwindow (V.getwindowtoken (),0); } }ReturnSuper.dispatchtouchevent (EV); }necessary, otherwise all the components will not be touchevent.if (GetWindow (). superdispatchtouchevent (EV)) {ReturnTrue }return ontouchevent (EV); }PublicBooleanIsshouldhideinput(View V, motionevent event) {if (v! =Null && (Vinstanceof EditText)) {Int[] Lefttop = {0,0};Gets the current location position of the input box V.getlocationinwindow (lefttop);int left = lefttop[0];int top = lefttop[1]; int bottom = top + v.getheight (); int right = left + v.getwidth (); if (Event.getx () > Left && event.getx () < right && event.gety () > Top && event.gety () < bottom) {//click on the input box area and leave the Click EditText event return FALSE;} else {//causes EditText to trigger a lost Focus event v.setfocusable (false); //v.setfocusable (TRUE);//This is not required because the following code will implement this function at the same time V.setfocusableintouchmode (true); return true;} return false;}        

Add the above code and you are pleasantly surprised to find that the problem seems to be a perfect solution. However, if you have more than one interface EditText , you will find that when you click on the first one EditText , the second one EditText will have the following cursor, or click the second or later EditText , the first one EditText will have a cursor flash. Through log you will find that from the second click EditText , each click EditText will first trigger a loss of focus, and then trigger a focus. This can infer that when there are multiple EditText , one EditText loses focus triggers another to get the focus. This problem must be resolved, and the presence of a screen cursor flashing affects the user experience.

Step three: Avoid another edittext get focus

The solution to this problem is to set the following property in the same XML for a control that will not respond, such as TextView :

android:focusable="true"android:focusableInTouchMode="true"


An elephant that can fly
Links: HTTPS://WWW.JIANSHU.COM/P/3D31D681F4BC
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

EditText Focus Issues 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.