Android basic getting started tutorial-3.5 listens to EditText content changes, androidedittext
Basic tutorial for Android-3.5 listening for EditText Content Changes
Tags (separated by spaces): basic Android tutorial
This section introduces:
We have learned the EditText control before. This section describes how to monitor the content changes in the input box!
This is very practical in actual development. In addition, it comes with instructions on how to make the EditText password visible.
And invisible! Now, start this section!
1. Listen for EditText Content Changes
According to the question, it is a listener-based event processing mechanism, as if the previous click event was OnClickListener, text content
The changed listener is TextWatcher. We can call EditText. addTextChangedListener (mTextWatcher );
Set content change listening for EditText!
To put it simply, TextWatcher requires three methods to implement this class:
public void beforeTextChanged(CharSequence s, int start,int count, int after); public void onTextChanged(CharSequence s, int start, int before, int count);public void afterTextChanged(Editable s);
It will be triggered in the following cases in turn:
1. Before content changes 2. During content changes 3. After Content Changes
We can rewrite related methods based on actual needs. Generally, the third method is much rewritten!
There are many scenarios for listening to EditText content changes:
Limit the number of words and content ~
Here we will provide you with a simple custom EditText. After you enter the content, a circle of the cross is displayed on the surface. After you click it
You can clear the text box ~, You can also add TextWatcher for EditText and set the delete button ~
Implementation:
Custom EditText:DelEditText. java
Package demo.com. jay. buttondemo; import android. content. context; import android. graphics. rect; import android. graphics. drawable. drawable; import android. text. editable; import android. text. textWatcher; import android. util. attributeSet; import android. view. motionEvent; import android. widget. editText;/*** Created by coder-pig on 2015/7/16 0016. */public class DelEditText extends EditText {private Drawable I MgClear; private Context mContext; public DelEditText (Context context, AttributeSet attrs) {super (context, attrs); this. mContext = context; init ();} private void init () {imgClear = mContext. getResources (). getDrawable (R. drawable. delete_gray); addTextChangedListener (new TextWatcher () {@ Override public void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Override public vo Id onTextChanged (CharSequence s, int start, int before, int count) {}@ Override public void afterTextChanged (Editable editable) {setDrawable ();}});} // draw and delete the image private void setDrawable () {if (length () <1) values (null, null); else setCompoundDrawablesWithIntrinsicBounds (null, null, imgClear, null);} // when the touch range is on the right side, the delete method is triggered to hide the cross @ Override public boolean o. NTouchEvent (MotionEvent event) {if (imgClear! = Null & event. getAction () = MotionEvent. ACTION_UP) {int eventX = (int) event. getRawX (); int eventY = (int) event. getRawY (); Rect rect = new Rect (); getGlobalVisibleRect (rect); rect. left = rect. right-100; if (rect. contains (eventX, eventY) setText ("");} return super. onTouchEvent (event) ;}@ Override protected void finalize () throws Throwable {super. finalize ();}}
Background drawable of EditText:Bg_frame_search.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="@color/background_white" /> <corners android:radius="5dp" /> <stroke android:width="1px" android:color="@color/frame_search"/></shape>
Color Resource: color. xml
<?xml version="1.0" encoding="utf-8"?><resources> <color name="reveal_color">#FFFFFF</color> <color name="bottom_color">#3086E4</color> <color name="bottom_bg">#40BAF8</color> <color name="frame_search">#ADAEAD</color> <color name="background_white">#FFFFFF</color> <color name="back_red">#e75049</color></resources>
Layout file:Activity_main.xml
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: background = "@ color/back_red" android: orientation = "vertical" tools: context = ". mainActivity "> <demo.com. jay. buttondemo. delEditText android: id = "@ + id/edit_search" android: layout_width = "match_parent" androi D: layout_height = "32dp" android: layout_margin = "10dp" android: background = "@ drawable/bg_frame_search" android: hint = "EditText ~ with the delete button ~ "Android: maxLength =" 20 "android: padding =" 5dp "android: singleLine =" true "/> </LinearLayout>
PS: the code is very simple and will not be explained ~
2. Make the EditText password visible and invisible
This is also a very practical requirement, that is, the user can click the button to make the password in EditText visible or invisible ~
Implementation:
Implementation Code:
Activity_main.xml
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "android: layout_margin =" 5dp "android: orientation =" horizontal "> <EditText android: id =" @ + id/edit_pawd "android: layout_width =" 0dp "android: layout_weight = "2" android: layout_height = "48dp" android: inputType = "textPassword" android: background = "@ drawable/editborder"/> <Button android: id = "@ + id/btnChange" android: layout_width = "0dp" android: layout_weight = "1" android: layout_height = "48dp" android: text = "password visible"/> </LinearLayout>
MainActivity. java
Package com. jay. demo. edittextdemo; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. text. method. hideReturnsTransformationMethod; import android. text. method. passwordTransformationMethod; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. button; import android. widget. editText; public class MainActivity extends AppCompatActivity {private EditText edit_pawd; private Button btnChange; private boolean flag = false; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); edit_pawd = (EditText) findViewById (R. id. edit_pawd); btnChange = (Button) findViewById (R. id. btnChange); edit_pawd.setHorizontallyScrolling (true); // set EditText not to wrap btnChange. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {if (flag = true) {edit_pawd.setTransformationMethod (HideReturnsTransformationMethod. getInstance (); flag = false; btnChange. setText ("invisible password");} else {edit_pawd.setTransformationMethod (PasswordTransformationMethod. getInstance (); flag = true; btnChange. setText ("password visible ");}}});}}
Editborder. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Set transparent background color --> <solid android: color = "# FFFFFF"/> <! -- Set a white border --> <stroke android: width = "1px" android: color = "# FFFFFF"/> <! -- Set the margin to make the space bigger --> <padding android: bottom = "5dp" android: left = "5dp" android: right = "5dp" android: top = "5dp"/> </shape>
Summary:
This section is here. Thank you ~
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.