开发者

EditText in Android?

Is there any way to restrict that only 2 digit开发者_Go百科s 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) {}
});


You can add validation to an EditText using a TextWatcher.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜