android monitors EditText for changes

Keywords: Android xml encoding

Use the addTextChangedListener(TextWatcher watcher) method of EditText 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 will be triggered, and the abstract method in TextWatcher will be called.
Layout:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6. <EditText  
  7.     android:id="@+id/main_et"   
  8.     android:layout_width="fill_parent"     
  9.     android:layout_height="wrap_content" />   
  10. <TextView  
  11.     android:id="@+id/main_tv"   
  12.     android:layout_width="fill_parent"     
  13.     android:layout_height="wrap_content" />   
  14. </LinearLayout>  
1, Monitor the number of text boxes and prompt how many characters can be input
MainActivity:
  1. package chay.mian;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.text.Editable;  
  6. import android.text.TextWatcher;  
  7. import android.view.View;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.     private EditText editText;  
  14.     private TextView tip;  
  15.     private final int charMaxNum = 10//Number of words allowed  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         init();  
  22.     }  
  23.   
  24.     private void init() {  
  25.         editText = (EditText) findViewById(R.id.main_et);  
  26.         editText.addTextChangedListener(new EditChangedListener());  
  27.           
  28.         tip = (TextView) findViewById(R.id.main_tv);  
  29.         tip.setText("0/"+charMaxNum);  
  30.     }  
  31.   
  32.     class EditChangedListener implements TextWatcher {  
  33.         private CharSequence temp; //Text before listening  
  34.         private int editStart; //Cursor start position  
  35.         private int editEnd; //Cursor end position  
  36.           
  37.         //Status before entering text  
  38.         @Override  
  39.         public void beforeTextChanged(CharSequence s, int start, int count,int after) {  
  40.             temp = s;  
  41.         }  
  42.   
  43.         //Enter the status in the text, count is the number of characters entered at one time  
  44.         @Override  
  45.         public void onTextChanged(CharSequence s, int start, int before, int count) {  
  46. //          if (charMaxNum - s.length() <= 5) {  
  47. //tip.setText("can also input" + (charMaxNum - s.length()) + "character");  
  48. //          }  
  49.             tip.setText((s.length()) + "/" + charMaxNum);  
  50.         }  
  51.   
  52.         //Status after entering text  
  53.         @Override  
  54.         public void afterTextChanged(Editable s) {  
  55.             /** Get the start and end position of the cursor, record the number index just exceeded after the maximum number for control*/  
  56.             editStart = editText.getSelectionStart();  
  57.             editEnd = editText.getSelectionEnd();  
  58.             if (temp.length() > charMaxNum) {  
  59. //Toast.makeText(getApplicationContext(), "input up to 10 characters", Toast.LENGTH_SHORT).show();  
  60.                 s.delete(editStart - 1, editEnd);  
  61.                 editText.setText(s);  
  62.                 editText.setSelection(s.length());  
  63.             }  
  64.         }  
  65.     };  
  66. }  


2, For data type inspection, when the input is not an integer number, the input box will pop up immediately and prompt for correction
MainActivity:

  1. package chay.mian;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.DialogInterface;  
  6. import android.os.Bundle;  
  7. import android.text.Editable;  
  8. import android.text.TextWatcher;  
  9. import android.util.Log;  
  10. import android.widget.EditText;  
  11. import android.widget.TextView;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     private EditText editText;  
  16.     private TextView tip;  
  17.     String str;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_mian);  
  23.         init();  
  24.     }  
  25.   
  26.     private void init() {  
  27.         editText = (EditText) findViewById(R.id.mian_et);  
  28.         editText.addTextChangedListener(new EditChangedListener());  
  29.         tip = (TextView) findViewById(R.id.mian_tv);  
  30.         tip.setText("Please enter an integer number");  
  31.     }  
  32.   
  33.     class EditChangedListener implements TextWatcher {  
  34.   
  35.         //Status before entering text  
  36.         @Override  
  37.         public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
  38.             Log.d("TAG""beforeTextChanged--------------->");  
  39.         }  
  40.   
  41.         //Enter the status in the text, count is the number of characters entered at one time  
  42.         @Override  
  43.         public void onTextChanged(CharSequence s, int start, int before, int count) {  
  44.             Log.d("TAG""onTextChanged--------------->");  
  45.         }  
  46.   
  47.         //Status after entering text  
  48.         @Override  
  49.         public void afterTextChanged(Editable s) {  
  50.             Log.d("TAG""afterTextChanged--------------->");  
  51.             str = editText.getText().toString();  
  52.             try {  
  53. //              if ((editText.getText().toString()) != null)  
  54.                     Integer.parseInt(str);  
  55.             } catch (Exception e) {  
  56.                 showDialog();  
  57.             }  
  58.         }  
  59.     };  
  60.   
  61.     private void showDialog() {  
  62.         AlertDialog dialog;  
  63.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  64.         builder.setTitle("news").setIcon(android.R.drawable.stat_notify_error);  
  65.         builder.setMessage("The integer number you output is wrong, please correct it");  
  66.         builder.setPositiveButton("Determine"new DialogInterface.OnClickListener() {  
  67.             @Override  
  68.             public void onClick(DialogInterface dialog, int which) {  
  69.                 // TODO Auto-generated method stub  
  70.             }  
  71.         });  
  72.         dialog = builder.create();  
  73.         dialog.show();  
  74.     }  
  75. }  

View the order in which these methods are called in LogCat:
beforeTextChanged-->onTextChanged-->onTextChanged


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

Posted by cupboy on Wed, 22 Apr 2020 08:47:14 -0700