TextView editEntryView = new TextView(...);InputFilter[] filterArray = new InputFilter[1];filterArray[0] = new InputFilter.LengthFilter(8);editEntryView.setFilters(filterArray);
import android.content.Context;import android.text.InputFilter;import android.text.InputType;import android.widget.EditText;
public class EditTextNumeric extends EditText {protected int max_value = Integer.MAX_VALUE;protected int min_value = Integer.MIN_VALUE;
// constructorpublic EditTextNumeric(Context context) {super(context);this.setInputType(InputType.TYPE_CLASS_NUMBER);}
// checks whether the limits are set and corrects them if not within limits@Overrideprotected void onTextChanged(CharSequence text, int start, int before, int after) {if (max_value != Integer.MAX_VALUE) {try {if (Integer.parseInt(this.getText().toString()) > max_value) {// change value and keep cursor positionint selection = this.getSelectionStart();this.setText(String.valueOf(max_value));if (selection >= this.getText().toString().length()) {selection = this.getText().toString().length();}this.setSelection(selection);}} catch (NumberFormatException exception) {super.onTextChanged(text, start, before, after);}}if (min_value != Integer.MIN_VALUE) {try {if (Integer.parseInt(this.getText().toString()) < min_value) {// change value and keep cursor positionint selection = this.getSelectionStart();this.setText(String.valueOf(min_value));if (selection >= this.getText().toString().length()) {selection = this.getText().toString().length();}this.setSelection(selection);}} catch (NumberFormatException exception) {super.onTextChanged(text, start, before, after);}}super.onTextChanged(text, start, before, after);}
// set the max number of digits the user can enterpublic void setMaxLength(int length) {InputFilter[] FilterArray = new InputFilter[1];FilterArray[0] = new InputFilter.LengthFilter(length);this.setFilters(FilterArray);}
// set the maximum integer value the user can enter.// if exeeded, input value will become equal to the set limitpublic void setMaxValue(int value) {max_value = value;}// set the minimum integer value the user can enter.// if entered value is inferior, input value will become equal to the set limitpublic void setMinValue(int value) {min_value = value;}
// returns integer value or 0 if errorous valuepublic int getValue() {try {return Integer.parseInt(this.getText().toString());} catch (NumberFormatException exception) {return 0;}}}
示例用法:
final EditTextNumeric input = new EditTextNumeric(this);input.setMaxLength(5);input.setMaxValue(total_pages);input.setMinValue(1);
/*** This sets the maximum length in characters of an EditText view. Since the* max length must be done with a filter, this method gets the current* filters. If there is already a length filter in the view, it will replace* it, otherwise, it will add the max length filter preserving the other** @param view* @param length*/public static void setMaxLength(EditText view, int length) {InputFilter curFilters[];InputFilter.LengthFilter lengthFilter;int idx;
lengthFilter = new InputFilter.LengthFilter(length);
curFilters = view.getFilters();if (curFilters != null) {for (idx = 0; idx < curFilters.length; idx++) {if (curFilters[idx] instanceof InputFilter.LengthFilter) {curFilters[idx] = lengthFilter;return;}}
// since the length filter was not part of the list, but// there are filters, then add the length filterInputFilter newFilters[] = new InputFilter[curFilters.length + 1];System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length);newFilters[curFilters.length] = lengthFilter;view.setFilters(newFilters);} else {view.setFilters(new InputFilter[] { lengthFilter });}}
<EditTextandroid:id="@+id/edtUserCode"android:layout_width="wrap_content"android:layout_height="wrap_content"android:maxLength="4"android:hint="Enter user code" />
private class EditTextWatcher(private val view: EditText) : TextWatcher {private var position = 0private var oldText = ""
override fun afterTextChanged(s: Editable?) = Unit
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {oldText = s?.toString() ?: ""position = view.selectionStart}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {val newText = s?.toString() ?: ""if (newText.length > 10) {with(view) {setText(oldText)position = if (start > 0 && count > 2) {// Text paste in nonempty field.start} else {if (position in 1..10 + 1) {// Symbol paste in the beginning or middle of the field.position - 1} else {if (start > 0) {// Adding symbol to the end of the field.start - 1} else {// Text paste in the empty field.0}}}setSelection(position)}}}}
// Usage:editTextWatcher = EditTextWatcher(view.edit_text)view.edit_text.addTextChangedListener(editTextWatcher)