問題描述
我想知道如何對 EditText 進行驗證.例如,我有一個只應接受數值的 EditText.如果用戶輸入的不是數字值,那么它應該顯示一條警告消息(即請使用數字值....").
I would like to know how to make a validation on EditText. For example, I have one EditText that should only accept numeric values. If something other than a numeric value is typed by the user then it should show an alert message (i.e. "please use a numeric value....").
是否有功能可用于確定輸入的文本是否為特定類型?如果可能,請附上代碼片段.
Is there a function available to find out if the entered text is particular type? If possible please include a code snippet.
推薦答案
與其制作彈出窗口,不如將提示集成到 EditText 中,這樣用戶只能在 EditText 中鍵入數字(android:數字,機器人:提示):
Rather than make a pop-up I would integrate a hint into the EditText and I would make it so the user could only type numbers into the EditText (android:numeric, android:hint):
<EditText android:layout_height="wrap_content"
android:numeric="integer"
android:hint="@string/numberHint"
android:gravity="left"
android:id="@+id/name"
android:layout_width="wrap_content"
android:maxWidth="60dp"
android:textSize="6pt">
</EditText>
更多信息可在此處獲得:http://developer.android.com/reference/android/widget/EditText.html
More information is available here: http://developer.android.com/reference/android/widget/EditText.html
這篇關于驗證編輯文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!