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:
- <Edittext
- Android: layout_width = "fill_parent"
- Android: layout_height = "36dip"
- Android: Background = "@ drawable/bg_edittext"
- Android: padding = "5dip"
- Android: layout_margin = "36dip"
- Android: textcolorhint = "# aaaaaa"
- Android: textsize = "15dip"
- Android: singleline = "true"
- Android: hint = "Enter ..."
- />
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)
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Shape xmlns: Android = "http://schemas.android.com/apk/res/android">
- <Solid Android: color = "# ffffff"/>
- <Corners Android: radius = "3dip"/>
- <Stroke
- Android: width = "1dip"
- Android: color = "# bdc7d8"/>
- </Shape>
Bg_edittext_focused.xml (when getting Focus)
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Shape xmlns: Android = "http://schemas.android.com/apk/res/android">
- <Solid Android: color = "# ffffff"/>
- <Corners Android: radius = "3dip"/>
- <Stroke
- Android: width = "1dip"
- Android: color = "#728ea3"/>
- </Shape>
Bg_edittext.xml (selector, which is widely used on the Internet)
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
- <Item Android: state_window_focused = "false" Android: drawable = "@ drawable/contact_edit_edittext_normal"/>
- <Item Android: state_focused = "true" Android: drawable = "@ drawable/contact_edit_edittext_focused"/>
- </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:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Resources>
- <Style name = "my_edittext_style" parent = "@ Android: style/widget. edittext">
- <Item name = "Android: background"> @ drawable/my_edittext </item>
- </Style>
- </Resources>
Next, add my_edittext.xml to drawable: The content is as follows:
[Java] view plaincopy
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
- <Item Android: state_focused = "true" Android: drawable = "@ drawable/editbox_focus"/>
- <Item Android: drawable = "@ drawable/editbox_normal"/>
- </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
- <Edittext
- Style = "@ style/my_edittext_style"
- Android: text = "My edittext"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
- </Edittext>
Because you can set the parent attribute in the style.