EditText loses focus in Android, EditText disables pop-up keyboard

Source: Internet
Author: User

In our app, sometimes when you enter a page, EditText automatically gets the focus by default. Pop-up input box, the user experience is not good,

So how do you cancel this default behavior?

PS: This text is written a year ago, now there are netizens to ask this question, I have to re-edit--2014.05.07, there is a better way, the first method is very limited, we can use the second method

The first method: in the online search for a long time, a bit of monitoring the soft keyboard event method, there is call Clearfouse () method, but the test is not! The corresponding attribute could not be found in the corresponding XML to close the default behavior.

Later, under its parent control, add the following attributes, which can be perfectly resolved:

Android:focusable= "true"
Android:focusableintouchmode= "true"

Examples are as follows:

<linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:ori entation= "Horizontal" android:focusable

= "true" Android:focusableintouchmode= "true"

> <= "@+id/et_enter_msg_content" = "wrap_content" = "wrap_content" = "1"/> < = "@+id/sent" = "wrap_content" = "wrap_content" = "@string/send"/> </LinearLayout>

second method: directly close the IME

1 2 3 4 5 6 7 8 private   void   closeinputmethod () {      Inputmethodmanager imm = (inputmethodmanager) getsystemservice (context.input_method_service);      boolean   isOpen = imm.isactive ();      if   (isOpen) {          Imm.togglesoftinput (0, inputmethodmanager.hide_not_always);//Display if not displayed           Imm.hidesoftinputfromwindow (Mobile_topup_num.getwindowtoken (), Inputmethodmanager.hide_not_always);      } }

Call this method body on the line, the specific if statement inside a few parameters, I borrowed a netizen's log to write (thanks in this)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示)  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  imm.toggleSoftInput( 0 , InputMethodManager.HIDE_NOT_ALWAYS);   2 、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)   InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);  [java] view plaincopy imm.hideSoftInputFromWindow(view.getWindowToken(), 0 ); //强制隐藏键盘  3 、调用隐藏系统默认的输入法  ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity. this .getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  (WidgetSearchActivity是当前的Activity)  4 、获取输入法打开的状态  InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  booleanisOpen=imm.isActive(); //isOpen若返回true,则表示输入法打开 

All right, I hope you all have fun.


EditText loses focus in Android, EditText disables pop-up keyboard

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.