EditText focus control, edittext focus

Source: Internet
Author: User

EditText focus control, edittext focus

1. by default, EditText gets the focus, which in some cases hinders the functions implemented by developers, such as the Development search function. After EditText is added, the page is displayed by default, editText also gets the focus, but the user does not have the search function in the actual application. To achieve EditText, the focus is lost by default. The method is as follows:

Add in the EditText parent container

Android: focusable = "true"
Android: focusableInTouchMode = "true"

You can. For example:

<FrameLayout
Android: layout_width = "match_parent"
Android: layout_height = "@ dimen/fragment_look_list_et_layout_height"
Android: layout_marginTop = "1dip"
Android: background = "# f0f0f0"
Android: focusable = "true"
Android: focusableInTouchMode = "true"

Android: gravity = "center"
Android: paddingLeft = "@ dimen/fragment_look_list_et_padding"
Android: paddingRight = "@ dimen/fragment_look_list_et_padding">


<EditText
Android: id = "@ + id/et_search"
Android: layout_width = "match_parent"
Android: layout_height = "@ dimen/fragment_look_list_et_height_width"
Android: layout_gravity = "center"
Android: singleLine = "true"
Android: ellipsize = "start"
Android: background = "@ drawable/shape_look_list_et_bg"
Android: gravity = "center_vertical"
Android: textSize = "14sp"
Android: paddingRight = "@ dimen/fragment_look_list_et_padding_right"
Android: paddingLeft = "@ dimen/fragment_look_list_et_padding_left"/>

</FrameLayout>

2. after solving the problem of focus in the default situation, we usually want to use code to dynamically control the loss of focus (it's easy to get the focus, just click it ), for example, there is a ListVIew list under the search box. When we slide the list, we hope that EditText will lose the focus, as follows:

(1) first, add android: windowSoftInputMode = "stateHidden | adjustResize" to the AndroidManiFest. xml activity"

For example:

<Activity
Android: name = ". activity. MainActivity"
Android: label = "@ string/app_name"
Android: windowSoftInputMode = "stateHidden | adjustResize"
Android: screenOrientation = "portrait">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

(2) EditText loses focus code:

// EditText loses focus
SearchET. clearFocus ();



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.