Determine the number of EditText characters that can be input with custom controls in Android
A requirement for a recent project is to determine the number of characters that can be entered, that is, to listen to the text changes in EditText. The function is implemented, but the combination method is used, it is better to write a custom control every time. In view this article, it is recommended to first refer to I reproduced an article: http://blog.csdn.net/android_jiangjun/article/details/39580253
The topic of this article is as follows:
First, let's take a look:
The custom controls in this article use the composite controls to customize controls. The layout of custom controls is as follows:
Then the corresponding custom HintEdit class code is as follows:
Package com. gc. testcustomedittext; import android. annotation. suppressLint; import android. content. context; import android. content. res. typedArray; import android. text. editable; import android. text. inputFilter; import android. text. textWatcher; import android. util. attributeSet; import android. view. layoutInflater; import android. widget. editText; import android. widget. linearLayout; import android. widget. relativeLayout; import android. widget. textView;/***** @ author Android General **/public class HintEditText extends RelativeLayout implements TextWatcher {private EditText mEditText; private TextView mTextView; private int maxLength = 0; private RelativeLayout comment; @ SuppressLint (NewApi) public HintEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} public HintEditText (Context context) {super (context);} public HintEditText (Context context, AttributeSet attrs) {super (context, attrs); TypedArray mTypedArray = context. obtainStyledAttributes (attrs, R. styleable. hintEditText); maxLength = mTypedArray. getInt (R. styleable. hintEditText_maxLength, 0); mRelativeLayout = (RelativeLayout) LayoutInflater. from (context ). inflate (R. layout. custom_edittext, this, true); mEditText = (EditText) mRelativeLayout. findViewById (R. id. edit); mTextView = (TextView) mRelativeLayout. findViewById (R. id. text); mTextView. setHint (you can also enter + maxLength + words); // you can specify the maximum number of mEditText characters that can be entered. setFilters (new InputFilter [] {new InputFilter. lengthFilter (maxLength)}); mEditText. addTextChangedListener (this);} public void initUI (Context context) {RelativeLayout mRelativeLayout = (RelativeLayout) LayoutInflater. from (context ). inflate (R. layout. custom_edittext, this, true); mEditText = (EditText) mRelativeLayout. findViewById (R. id. edit); mTextView = (TextView) mRelativeLayout. findViewById (R. id. text) ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {// TODO Auto-generated method stub} @ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {// TODO Auto-generated method stubmTextView. setHint (can also be input + (maxLength-s.toString (). length () + word);} @ Overridepublic void afterTextChanged (Editable s) {// TODO Auto-generated method stub }}
The code for the attrs. xml file is as follows:
The file code for the main layout is as follows:
The MainActivity code is as follows:
Package com. gc. testcustomedittext; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem;/***** @ author Android General **/public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main );}}