Custom settings of the edittext style (custom border and border color)

Source: Internet
Author: User

Edittext changes the border color

Step 1: For better comparison, prepare two identical edittexts (when the activity starts, the focus will be on the first edittext, if you do not want to write an edittext with a height and a bandwidth of 0, you can avoid it. here we will not do this). The Code is as follows:

 

  1. <Edittext
  2. Android: layout_width = "fill_parent"
  3. Android: layout_height = "36dip"
  4. Android: Background = "@ drawable/bg_edittext"
  5. Android: padding = "5dip"
  6. Android: layout_margin = "36dip"
  7. Android: textcolorhint = "# aaaaaa"
  8. Android: textsize = "15dip"
  9. Android: singleline = "true"
  10. Android: hint = "Enter ..."
  11. />


Next, create three XML files: the background when the input box does not obtain the focus, the background when the input box gets the focus, and the selector background selector (here we can obtain the time when the input box gets and loses the focus ), the Code is as follows:

Bg_edittext_normal.xml (when no focus is obtained)

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Shape xmlns: Android = "http://schemas.android.com/apk/res/android">
  3. <Solid Android: color = "# ffffff"/>
  4. <Corners Android: radius = "3dip"/>
  5. <Stroke
  6. Android: width = "1dip"
  7. Android: color = "# bdc7d8"/>
  8. </Shape>

Bg_edittext_focused.xml (when getting Focus)

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Shape xmlns: Android = "http://schemas.android.com/apk/res/android">
  3. <Solid Android: color = "# ffffff"/>
  4. <Corners Android: radius = "3dip"/>
  5. <Stroke
  6. Android: width = "1dip"
  7. Android: color = "#728ea3"/>
  8. </Shape>

Bg_edittext.xml (selector, which is widely used on the Internet)

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
  3. <Item Android: state_window_focused = "false" Android: drawable = "@ drawable/contact_edit_edittext_normal"/>
  4. <Item Android: state_focused = "true" Android: drawable = "@ drawable/contact_edit_edittext_focused"/>
  5. </Selector>


This is OK, as shown below:

 

The border of the second input box becomes dark. Is it more friendly.

Edittext custom border settings

There are two ways to achieve the effect: one is to use graphs as the background, and the other is to use self-painting.

The following figure shows the background:
First, redefine a style. Create a style. xml file in the values Folder:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Resources>
  3. <Style name = "my_edittext_style" parent = "@ Android: style/widget. edittext">
  4. <Item name = "Android: background"> @ drawable/my_edittext </item>
  5. </Style>
  6. </Resources>

Next, add my_edittext.xml to drawable: The content is as follows:

[Java] view plaincopy

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
  3. <Item Android: state_focused = "true" Android: drawable = "@ drawable/editbox_focus"/>
  4. <Item Android: drawable = "@ drawable/editbox_normal"/>
  5. </Selector>


Editbox_normal indicates the normal edit box image, and editbox_focus indicates the selected edit box image.

After defining these two files, you can use the following methods:

[Java] view plaincopy

  1. <Edittext
  2. Style = "@ style/my_edittext_style"
  3. Android: text = "My edittext"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "wrap_content">
  6. </Edittext>

Because you can set the parent attribute in the style.

 

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.