簡體   English   中英

android中的EditText(貨幣)驗證

[英]EditText (Currency) validation in android

我想設置EditText驗證,

僅包含數字,小數位數為4位數,小數值包含2位數...總計6位數,

我也想要只輸入數字表格編號。 如果我選擇任何非數字編號,那么EditText將不接受它。

如果有任何建議,那就太好了。 謝謝。

android:inputType="numberDecimal"

EditText屬性

您可以將正則表達式用於驗證部分。 以下只是一個樣本

您可以使用在線正則表達式測試人員查看@ http://regex101.com/r/uG9aF6/1

String input = "1024.22";
String pattern = "([0-9]{4})(\.)([0-2]{2})"; // 4 digits followe by . followed by 2 digits
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
if(m.matches())
{
   System.out.println("Validated");
}
else
{
   System.out.println("Not Validated");
}

editText xml布局中, android:inputType ="number"android:maxLength="6"將彈出數字小鍵盤,editText只能輸入六位數字

像這樣

<EditText
        android:id="@+id/myinput"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:maxLength="6"
        android:hint="@string/somestring"
        android:layout_weight="1"
    />

嘗試這種模式^([0-9] {1,4})(。[0-9] {1,2})?$

它之前接受最多4位數字。 並且只有最多2次

您可以查看您的期間位置。 例如在你的情況下

char value = 'anychar' ; if (edittext.gettext.toString().indexOf(value) == 4 ){ // your logic goes here }

    <EditText
    android:id="@+id/edit1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:maxLength="7" />

數字為4位數,。(點)為1位數,分數為2位數。 當你專注於Edittext時,它也是Number Decimal SoftKeyboard。

如果你想專注於Edittext自動使用這個:

          android:focusable="true"

暫無
暫無

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

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