繁体   English   中英

Android中的EditText?

[英]EditText in Android?

有没有办法限制在运行时只能在EditText中输入2位数字。

例如:

如果我设置我的android:inputType="numberDecimal" ,我输入其中的值。 它接受值123.000000000000。 但我想限制它像123.00

有没有可能的方法呢?

看一下以下帖子,也许对你有所帮助:

防止输入

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添加验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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