問(wèn)題描述
我已閱讀 Android:將 EditText 限制為數(shù)字 和 如何在 android 的 EditText 上顯示數(shù)字鍵盤(pán)?.不幸的是,它們似乎都不符合我的需求.
I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.
我想將我的 EditText 輸入限制為僅限數(shù)字.但是,我也希望允許有符號(hào)和/或十進(jìn)制輸入.
I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.
這是我當(dāng)前的代碼(我需要以編程方式執(zhí)行此操作):
Here is my current code (I need to do this programmatically):
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
有了這個(gè),我的 EditText 愉快地將所有輸入限制為數(shù)字.不幸的是,它不允許任何其他內(nèi)容,例如小數(shù)點(diǎn).
With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.
如果我將該行更改為 edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
,EditText 將接受所有輸入(這不是我想要的......).
If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
, the EditText accepts all input (which isn't what I want...).
我嘗試過(guò)組合標(biāo)志(絕望地想看看它是否可行):
I've tried combining flags (in desperation to see if it would work):
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)
這也不起作用(EditText 像往常一樣接受所有輸入).
That didn't work either (the EditText accepted all input as usual).
那么,我該怎么做呢?
推薦答案
嘗試使用 TextView.setRawInputType()對(duì)應(yīng)android:inputType
屬性.
這篇關(guān)于如何將我的 EditText 輸入限制為數(shù)字(可能是十進(jìn)制和有符號(hào))輸入?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!