簡體   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