EditText style in Android change focus cursor, background

Source: Internet
Author: User

In Android development, depending on the needs of the project, some special styles need to be customized, such as: When using EditText, the background of the focus and the cursor picture are used by custom instead of the Android system default. These two days, this requirement is covered in the project and is now recorded as follows:

First of all, the inspiration comes from the comments in http://bbs.csdn.net/topics/391491663, thank you! Also, for more information on EditText properties, refer to: http://blog.csdn.net/qq_15128547/article/details/50947041

By default:

<edittext
Android:id= "@+id/edittext"
Android:layout_width= "120DP"
android:layout_height= "Wrap_content"
android:layout_marginleft= "20DP"
Android:gravity= "left"
android:hint= "Hint"
/>

The default performance is as follows:

Observation results found that by default, the focus background blue, the cursor black, the selection below is a blue picture (that is, the textselecthandle in the figure)

The above three items are now customized and require the following three attributes:

Android:background background

android:textcursordrawable cursor

Android:textselecthandle Focus Selection Icon

Settings are as follows:

<edittext
            android:id= "@+id/edittext "
            android:layout_width=" 120DP "
             android:layout_height= "Wrap_content"
             android:layout_marginleft= "20DP"
             android:gravity= "left"
             android:hint= "hint"
             android:background= "@drawable/edit_text_bg"
             android:textcursordrawable= "@drawable/edit_cursor"
             android:textselecthandle= "@drawable/edit_select_handle"/>

After setting, the effect is as follows:

Below, describe the corresponding style picture separately:

EDIT_TEXT_BG: Customizing the background
Edit_cursor: Cursor
Edit_select_handle: Focus Select Icon Below

The above three, all use the custom drawable, the specific code is as follows:
Edit_text_bg.xml:

1<?xml version= "1.0" encoding= "Utf-8"?>2<inset xmlns:android= "Http://schemas.android.com/apk/res/android"3>4<selector>5<item android:state_enabled= "false" >6<nine-patch android:src= "@drawable/edittext_thin"7android:tint= "@color/n_text_default_color"8/> <!--tint: shader Example: The current edittext_src is a black line, and when displayed, because tint is used, the color is reset, so it Set the line Color--9</item>Ten<item android:state_pressed= "false" android:state_focused= "false" > One<nine-patch android:src= "@drawable/edittext_thin" Aandroid:tint= "@color/n_text_default_color"/> -</item> -<item> the<nine-patch android:src= "@drawable/edittext_thin" -android:tint= "@color/n_text_focus_color"/> -</item> -</selector> +</inset>
View Code

Edit_cursor.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"     android:shape= "Rectangle" >        <size android:width= "2DP"/> <solid        android:color= "@ Color/n_text_focus_color "/></shape>

Edit_select_handle.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"     android:shape= "Rectangle" >    <size android:width= "0DP"/>            <!--is not actually displayed, the image is only a placeholder, Due to the direct setting android:textselecthandle= "@null" Run error, of course, depending on the situation, can be set to a picture PNG and other--></shape>

The entire modification involves the code as above, but it is important to note:

1, if only one edittext need to use a special style is directly in the current EditText to set properties, but for extensibility, it is recommended to write in Styles.xml, later in the edittext required to refer to the style, that is:

<Edittext...style= "@style/edittextstyle" ... />

2, if the whole project is unified style, then in the Androidmanifest.xml <application> tag, add android:theme= "@style/apptheme", The following attributes are added to the apptheme:

<style name= "Apptheme" parent= "@style/appbasetheme" >     ... <item name= "Android:edittextstyle" > @style/edittextstyle</item>    ... </style>

3. The style of Edittextstyle is as follows:

<!--edittextstyle  edittext style settings: Background, cursor, select icon below-      <style  name= "Edittextstyle" parent= "Android : Widget.Material.Light.EditText ">              <item name=" Android:background "> @drawable/edit_text_bg</item >       <item name= "android:textcursordrawable" > @drawable/edit_cursor</item>       <item name= " Android:textselecthandle "> @drawable/edit_select_handle</item>    </style >   

<!-- about the above parent= "Android:Widget.Material.Light.EditText", need to according to their current theme theme settings; in fact, there is not much difference, look at the source of the discovery of the final parent= "Widget.edittext"-

--------------------------Add the following, another picture, when selecting text, the icon that appears:

Android also provides the corresponding properties, which are not mentioned here:

android:textselecthandleleft= "@drawable/edit_select_left" // left // right
View Code

Doubt: When writing code, use to hint, but, at first run, did not show, many changes also not, finally, a very previous interface comparison, found that the current activity is not set theme, so set up the system theme, the results can be, do not know what to ask? Know the little buddy, Trouble Tell:

<style name= "Appbasetheme" parent= "Android:Theme.Light" >           </style>    <style name= "Apptheme" parent= "@style/appbasetheme" ></style>

This article is only a point, about the EditText attribute also many, we can test, what problems, welcome to communicate!

EditText style in Android change focus cursor, background

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.