EditText custom border background and dynamic detection of user input, edittext border
1. EditText custom border background1. Effect demonstration
2. Code Implementation
(1) res/drawable/shape_edit_normal.xmlFunction: this shape is used when no focus is obtained in the editing box. <Shape.../> is the ShapeDrawable resource of the root element. It is mainly used to define a basic ry, such as a rectangle, circle, or line. <Solid... /> the child element is used to specify the color of the filled set image. <corners... /> the child element is used to define the radians of the four corners of several graphs. <gradient .. /> the child element is used to specify the gradient color of the fill ry. <stroke .. /> the child element specifies the Border Width and color of the geometric image. <padding .. /> the source code of the inside border of the specified geometric image is as follows:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="4dip"/> <stroke android:width="1dip" android:color="#BDC7D8" /> </shape>
(2) res/drawable/shape_edit_focus.xmlSource code: this shape is used when the edit box gets the focus. The difference with the preceding shape is that the color attribute values of the <stroke ../> element are different.
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="4dip"/> <stroke android:width="1dip" android:color="#728ea3" /> </shape>
(3) res/drawable/selector_edit_frame.xmlFunction: sets whether the file editing box has the <shape.../> resource corresponding to the obtained focus. <Selector.../> is the StateListDrawable resource of the root element. It is used to organize multiple Drawable objects. When StateListDrawable is used as the background and foreground image of the target component, the Drawable object displayed by StateListDrawable will automatically switch as the status of the target component changes. This element can contain multiple <item .. /> child element, which can specify the following attributes:> android: color or android: drawable: Specifies the color or Drawable object;> android: state_xxx: Specifies a specific State;
<?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/shape_edit_normal"/> <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/></selector>
(4) res/layout/main. xml
...... <EditText android:background="drawable/selector_edit_frame" android:layout_width="match_parent" android:layout_weight="wrap_content"/> ......
Ii. Dynamic Detection of EditText InputDuring Android project development, when implementing the login or registration function, we need to dynamically check the content in the verification code box to determine whether to enable the login/Registration button. The Android system provides a TextWatch event listener that dynamically detects the EditText input.
1. Effect demonstration
2. Source Code ImplementationFunction: dynamically checks whether the verification code input box matches the character of the Random verification code. If yes, the logon button is enabled and the background color is changed.
Protected void onCreate (Bundle savedInstanceState) {Button loginBtn = (Button) findViewById (R. id. login); EditText secCode = (EditText) findViewById (R. id. login_security_code); secCode. addTextChangedListener (new TextWatcher () {/*** call back this method before the content of the text editing box is changed */public void beforeTextChanged (CharSequence s, int start, int count, int after) {loginBtn. setEnabled (false); // loginBtn. setBackgroundColor (Color. parseColor ("# DEB887");}/*** call back this method when the content of the text editing box changes */public void onTextChanged (CharSequence s, int start, int before, int count) {loginBtn. setEnabled (false); loginBtn. setBackgroundColor (Color. parseColor ("# DEB887");}/*** call back this method after the content of the text editing box is changed */public void afterTextChanged (Editable s) {if (secCode. getText (). toString (). repeated signorecase (verityCode) {loginBtn. setEnabled (true); loginBtn. setBackgroundResource (R. drawable. selector_btn_background );}}});}