This article describes the Android programming method of implementing EditText word count monitoring and display. Share it for your reference, as follows:
When developing applications, the number of words users enter is often limited, such as posting comments or other things. Here is a simple demo
EditText et_content;//Define a text input boxTextView tv_num;// Used to display the remaining wordsint num = 10;//The maximum number of words limited
et_content = (EditText) findViewById(.et_content); tv_num = (TextView) findViewById(.tv_num); tv_num.setText("10");
The following is to add a listening listener to the EditText text box
et_content.addTextChangedListener(new TextWatcher() { private CharSequence temp; private int selectionStart; private int selectionEnd; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { temp = s; ("s="+s); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { int number = num - (); tv_num.setText("" + number); selectionStart = et_content.getSelectionStart(); selectionEnd = et_content.getSelectionEnd(); //("start="+selectionStart+",end="+selectionEnd); if (() > num) { (selectionStart - 1, selectionEnd); int tempSelection = selectionStart; et_content.setText(s); et_content.setSelection(tempSelection);//Set the cursor at the end } } });
This will be achieved
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android data skills for operating json format》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.