EditText loses focus in Android, EditText disables pop-up keyboard [turn]

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/yejiurui/archive/2013/01/02/2841945.html

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:o rientation= "Horizontal"         android:focusable
= "true" android:focusableintouchmode= "true"
        >                <edittext             android:id= "@+id/et_enter_msg_content"            android:layout_width= "Wrap_content            " android:layout_height= "Wrap_content"            android:layout_weight= "1"            />                <button             android:id= "@+ Id/sent "            android:layout_width=" wrap_content "            android:layout_height=" wrap_content "            android:text=" @string/send "            />                    </LinearLayout>


The second method: directly close the input method

12345678 privatevoidcloseInputMethod() {    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);    booleanisOpen = imm.isActive();    if(isOpen) {        // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示        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)

1234567891011121314151617181920 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 plaincopyimm.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,则表示输入法打开 

EditText loses focus in Android, EditText suppresses pop-up keyboard [go]

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.