简体   繁体   中英

EditText in Android?

Is there any way to restrict that only 2 digits can enter in the EditText at the Runtime.

For Example:

If i set my android:inputType="numberDecimal" and i am entering the value in it. It accepts the value 123.000000000000. But i want to restrict it like 123.00

Is there any Possible way to do this?

Take a look at the following post, maybe of some help for you:

Preventing input

EditText txtInput = (EditText) findViewById(R.id.txtInput);
txtInput.addTextChangedListener(new TextWatcher() 
{
    public void afterTextChanged(Editable edt) 
    {
        String temp = edt.toString();
        int posDot = temp.indexOf(".");
        if (posDot <= 0) return;
        if (temp.length() - posDot - 1 > 2)
        {
            edt.delete(posDot + 3, posDot + 4);
        }
    }

    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
});

您可以使用TextWatcherEditText添加验证。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM