Android listens for changes in EditText, androidedittext

Source: Internet
Author: User

Android listens for changes in EditText, androidedittext
The addTextChangedListener (TextWatcher watcher) method of EditText is used to monitor EditText. TextWatcher is an interface class, so the abstract method in TextWatcher must be implemented:

When the content in EditText changes, the TextChangedListener event is triggered and the abstract method in TextWatcher is called.
Layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" ><EditText    android:id="@+id/main_et"     android:layout_width="fill_parent"       android:layout_height="wrap_content" /> <TextView    android:id="@+id/main_tv"     android:layout_width="fill_parent"       android:layout_height="wrap_content" /> </LinearLayout>
1. Implement the number of words in the text box of the listener, prompting how many characters can be entered
MainActivity:
Package chay. mian; import android. app. activity; import android. OS. bundle; import android. text. editable; import android. text. textWatcher; import android. view. view; import android. widget. editText; import android. widget. textView; public class MainActivity extends Activity {private EditText editText; private TextView tip; private final int charMaxNum = 10; // Number of words allowed to be input @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init ();} private void init () {editText = (EditText) findViewById (R. id. main_et); editText. addTextChangedListener (new EditChangedListener (); tip = (TextView) findViewById (R. id. main_ TV); tip. setText ("0/" + charMaxNum);} class EditChangedListener implements TextWatcher {private CharSequence temp; // The text before listening private int editStart; // The cursor start position private int editEnd; // cursor end position // status before text input @ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {temp = s ;} // The status in the input text. count is the number of characters entered at one time @ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {// if (charMaxNum-s. length () <= 5) {// tip. setText ("can also input" + (charMaxNum-s. length () + "character"); //} tip. setText (s. length () + "/" + charMaxNum);} // enter the status after the text @ Overridepublic void afterTextChanged (Editable s) {/** to get the start and end positions of the cursor, after the maximum number is exceeded, record the number index that has just exceeded to control */editStart = editText. getSelectionStart (); editEnd = editText. getSelectionEnd (); if (temp. length ()> charMaxNum) {// Toast. makeText (getApplicationContext (), "up to 10 characters", Toast. LENGTH_SHORT ). show (); s. delete (editStart-1, editEnd); editText. setText (s); editText. setSelection (s. length ());}}};}


2. Data Type check. When the input is not an integer, the input box is immediately displayed, prompting you to correct the input.
MainActivity:

Package chay. mian; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. OS. bundle; import android. text. editable; import android. text. textWatcher; import android. util. log; import android. widget. editText; import android. widget. textView; public class MainActivity extends Activity {private EditText editText; private TextView tip; String str; @ Overridepr Otected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_mian); init ();} private void init () {editText = (EditText) findViewById (R. id. mian_et); editText. addTextChangedListener (new EditChangedListener (); tip = (TextView) findViewById (R. id. mian_ TV); tip. setText ("Please enter an integer");} class EditChangedListener implements TextWatcher {// The status before entering the text @ Override Public void beforeTextChanged (CharSequence s, int start, int count, int after) {Log. d ("TAG", "beforeTextChanged --------------->");} // The status in the input text. count indicates the number of characters entered at one time @ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {Log. d ("TAG", "onTextChanged --------------->");} // status after text input @ Overridepublic void afterTextChanged (Editable s) {Log. d ("TAG", "afterTextChanged ---------------> "); Str = editText. getText (). toString (); try {// if (editText. getText (). toString ())! = Null) Integer. parseInt (str) ;}catch (Exception e) {showDialog () ;}}; private void showDialog () {AlertDialog dialog; AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("message "). setIcon (android. r. drawable. stat_policy_error); builder. setMessage ("the integer number you output is incorrect. Correct it"); builder. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub}); dialog = builder. create (); dialog. show ();}}

Check the order of calling these methods in LogCat:
BeforeTextChanged --> onTextChanged


Welcome to http://blog.csdn.net/ycwol/article/details/46594275

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.