簡體   English   中英

Android:將 EditText 限制為數字

[英]Android: Limiting EditText to numbers

在應用程序的 java 部分創建 EditText 時,如何像在 xml 中那樣將其限制為數字? 例如:

new EditText(this);

設置像

<EditText
    android:id="@+id/h1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"/>

也許是這樣的?

EditText text = new EditText(this);
text.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

不推薦使用android:numeric ,請使用android:inputType="number"

轉到文檔並查看帶有 XML 屬性的表格。 它顯示了每個項目的等效代碼。

在這種情況下,它是setRawInputType

這通常會這樣做:

android:numeric="integer"

只需在xml文件中使用以下幾行

只接受數字放:

 android:inputType="number"

要限制輸入數字的長度:

android:maxLength="10"

只接受特定數字:

EX:1下面的行只接受 1 或 0。

android:digits="10"

EX:2下面的行只接受 2 或 3。

android:digits="23"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM