If you like chestnut series can pay attention to oh ~ all kinds of chestnut is coming in……


Chestnut convention, GIF first

Chestnut 1. GIF

Chestnut 2. GIF

Core code (AnFQNumEditText custom combination control)

  1. Let’s start by calling (isn’t that easy) XML

     <anfq.numedittext.lib.AnFQNumEditText
         android:id="@+id/anetDemo"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>Copy the code

    java

     anetDemo = (AnFQNumEditText) findViewById(R.id.anetDemo);
     anetDemo.setEtHint("Content")// Set the prompt text
             .setEtMinHeight(200)// Set the minimum height in px
             .setLength(50)// Set the total word count
             //TextView displays type (SINGULAR SINGULAR type)(PERCENTAGE PERCENTAGE type)
             .setType(AnFQNumEditText.SINGULAR)
             .setLineColor("#3F51B5")// Set the line color
             .show();Copy the code
  2. implementation

    1. First by LayoutInflater. From access to the layout, then the findViewById inside control, here in the three controls EditText, TextView, View, the View is the most below the horizontal line

       LayoutInflater.from(context).inflate(R.layout.anfq_num_edittext, this.true);
       etContent = (EditText) findViewById(R.id.etContent);
       tvNum = (TextView) findViewById(R.id.tvNum);
       vLine = findViewById(R.id.vLine);Copy the code
    2. It then provides methods for setting values such as the lower-right type (there are two types: singular and percentage), the maximum character length, the Hint of the EditText, and the color of the line.
    3. Set the listener TextWatcher for the EditText.

       public void afterTextChanged(Editable s) {
           editStart = etContent.getSelectionStart();
           editEnd = etContent.getSelectionEnd();
           // Remove the listener first, otherwise stack overflow will occur
           etContent.removeTextChangedListener(mTextWatcher);
           // Note that you can only length the entire EditText at a time, not the deleted individual characters
           // Since it is both Chinese and English, the calculateLength function returns 1 for each character
      
           // Truncate the input when the number of characters exceeds the limit
           while (calculateLength(s.toString()) > MaxNum) { 
               s.delete(editStart - 1, editEnd);
               editStart--;
               editEnd--;
           }
           // Restore the listener
           etContent.addTextChangedListener(mTextWatcher);
           setLeftCount();
       }
       /** Refresh the remaining input words */
       private void setLeftCount() {
           if(TYPES.equals(SINGULAR)){/ / type 1
               tvNum.setText(String.valueOf((MaxNum - getInputCount())));
           }else if(TYPES.equals(PERCENTAGE)){/ / type 2
               tvNum.setText(MaxNum-(MaxNum - getInputCount())+"/"+MaxNum); }}/** Get the user input */
       private long getInputCount() {
           return calculateLength(etContent.getText().toString());
       }
       /** * Count the number of words shared, one Chinese character = two English letters, one Chinese punctuation = two English punctuation * Note: this function does not apply to single characters, because each character is rounded to 1 * @param cs * @return */
       public static long calculateLength(CharSequence cs) {
           double len = 0;
           for (int i = 0; i < cs.length(); i++) {
               int tmp = (int) cs.charAt(i);
               if (tmp > 0 && tmp < 127) {
                   len += 1;
               } else{ len++; }}return Math.round(len);
       }Copy the code

Summary: This EditText control is often used in development, but if it is used in more than one place, it will do too much listening code, so it is wrapped for future use


The source address


Please indicate the source of reprintJane:Indifference to life