Cancel textchange listener event

I have a edittext with a textchange listener. In the listener I test if the input of the user is where I want to test if the input is within the min-max boundaries.

valueText.addTextChangedListener(new TextWatcher() {
    String backup = "0";

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (count > 0 && !s.equals(backup)) {
            if (Integer.parseInt(s.toString()) > getMaximum()) {
                valueText.setText(backup);
            }
        }
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        backup = s.toString();
    }

    public void afterTextChanged(Editable s) {
    }
});

At the moment I have this, but is there an easy way to 'cancel' the textchange event so nothing happens when the value is above the maximum?

Or can i use a inputfilter where I can set the min and max input?

Thx :)

EDIT: I found a solution here: http://tech.chitgoks.com/2011/06/27/android-set-min-max-value-an-edittext-accepts/

Answers


You can use android:maxLength on your EditText XML definition.


 InputFilter.LengthFilter mLenthFilter=new InputFilter.LengthFilter(25); // pls define yr max length mine was 25.
        valueText.setFilters(new InputFilter[]{mLenthFilter});

http://tech.chitgoks.com/2011/06/27/android-set-min-max-value-an-edittext-accepts/, with this you can set the min and max value of an edittext.


Need Your Help

“void value not ignored as it ought to be” - Qt/C++

c++ qt casting void

I have this simple "interface" for some plugins I want to develop, it looks like:

AngularJS : Change parent scope value from custom directive

angularjs angularjs-directive angularjs-scope

For some reason I can't make this work based on the other examples I've seen here on SO.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.