Android cancels EditText focus

Source: Internet
Author: User

If you have edittext in the page, you will get the focus by default, and if you do not want to get the focus when you enter the page, follow these steps:

1. Add properties to the outermost layer of the layout:

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

2. Add a tag to the outermost layout:

<requestfocus/>

The effect is:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:focusable= "true"
Android:focusableintouchmode= "true" >


<requestfocus/>

<edittext
Android:id= "@+id/et_content"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>

</RelativeLayout>

If you also want to lose focus when you click outside of EditText, you can add a click event to the layout:

Contentview.setontouchlistener (New Ontouchlistener () {
@Override
public boolean OnTouch (View V, motionevent event) {
if (event.getaction () = = Motionevent.action_down) {
Et_content.clearfocus ();
}
return false;
}
});

Contentview on the top of that layout file.

View Contentview = View.inflate (Getapplicationcontext (), r.layout.activity_xx, NULL);

Et_content is your edittext.

Android cancels EditText focus

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.