Android EditText limits five implementations of input characters, from entry to depth

Keywords: Android Design Pattern Interview Programmer

@Override

public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {



}



@Override

public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    String editable = evPwd.getText().toString();

    String regEx = "[^a-zA-Z0-9]";  //Only letters or numbers can be entered

    Pattern p = Pattern.compile(regEx);

    Matcher m = p.matcher(editable);

    String str = m.replaceAll("").trim();    //Delete characters that are not letters or numbers

    if(!editable.equals(str)){

        evPwd.setText(str);  //Sets the character of EditText

        evPwd.setSelection(str.length()); //Because the character is deleted, it is necessary to override the setting of the new cursor position

    }

}



Fourth: through InputFilter To achieve. realization InputFilter A filter called filter Methods.



public abstract CharSequence filter (

CharSequence source,   // Input text

int start,   // Start position

int end,   // End position

Spanned dest, / / currently displayed content

int dstart,   // Current start position

int dend / / current end position

);



  

be careful: **IntentFilter Is an array, that is, you can write multiple filter conditions!**  

The following implementation makes EditText Receive only characters (numbers, letters), Character.isLetterOrDigit Will also regard Chinese as Letter, So you should write a regular rule to judge whether it is Chinese.



evPwd.setFilters(new InputFilter[]{

new InputFilter() {

    @Override

    public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {

        String regex = "^[\u4E00-\u9FA5]+$";

        boolean isChinese = Pattern.matches(regex, charSequence.toString());

        if (!Character.isLetterOrDigit(charSequence.charAt(i)) || isChinese) {

            return "";

        }

        return null;

    }

}

});



  

Fifth:    use EditText of InputConnection Property to restrict input characters. New class inherited from EditText And cover onCreateInputConnection Function, in xml Use in LimitText replace EditText. 



public class LimitEditText extends EditText {

public LimitEditText(Context context) {

    super(context);

}



public LimitEditText(Context context, AttributeSet attrs) {

    super(context, attrs);

}



public LimitEditText(Context context, AttributeSet attrs, int defStyleAttr) {

    super(context, attrs, defStyleAttr);

}

Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code
Network disk: pan.baidu.com/s/1uXQ5fqJkNbGiaj5iXGzdrQ
Extraction code: sei4

summary

Development is object-oriented. Our job search should be more interview oriented. Even if entering a big factory is really just going to Ning screw, you have to learn to build a plane during the interview, don't you?

The author has worked in small factories, Huawei and OPPO for 13 years. He joined Alibaba in April last year and has been there until now. I have been to a big factory and interviewed many people. I know that most junior and intermediate Android engineers who want to improve their skills often grope and grow by themselves. The learning effect of fragmentation is inefficient, long and helpless.

Attached here are dozens of interview questions of Tencent, headlines, Alibaba, meituan and other companies related to the above technical system diagram. The technical points are sorted into videos and PDF s (in fact, it takes a lot of energy than expected), including knowledge context + many details. Due to the limited space, some of them are shown to you in the form of pictures.

I believe it will bring you a lot of harvest:

There are too many materials, and the full display will affect the length. For the time being, list these screenshots first

**[CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code](

)**

F92Tx-1631423519239)]

[external chain picture transferring... (img-yb1Akdnp-1631423519242)]

There are too many materials, and the full display will affect the length. For the time being, list these screenshots first

**[CodeChina open source project: Android learning notes summary + mobile architecture Video + big factory interview real questions + project actual combat source code](

)**

It's easy to be a programmer. Being an excellent programmer requires continuous learning. From a junior programmer to a senior programmer, from a junior architect to a senior architect, or to management, from a technical manager to a technical director, you need to master different abilities at each stage. You can get rid of your peers in your work and ability improvement by determining your career direction early.

Posted by artweb on Fri, 19 Nov 2021 14:11:25 -0800