EditText implements input restrictions and verification, while edittext implements verification.
EditText implements input restrictions and Verification
I. Methods
1) input restrictions
1. android: digits restricts that only lowercase abc can be entered.
Android: digits = "abc"
2. android: inputType limits that only numbers can be entered
Android: inputType = "number"
You can set various restrictions in android: inputType, such as email addresses.
2) Verification
Directly implemented through code
String s = et_verify_empty.getText (). toString ();
If (s = null | s. length () = 0 ){
Et_verify_empty.setError ("cannot be blank ");
}
Ii. code example
Code
Fry. ActivityDemo2
1 package fry; 2 3 4 import com. example. editTextDemo1.R; 5 6 import android. app. activity; 7 import android. graphics. bitmapFactory; 8 import android. OS. bundle; 9 import android. text. spannable; 10 import android. text. spannableString; 11 import android. text. textUtils; 12 import android. text. style. imageSpan; 13 import android. view. view; 14 import android. view. view. onClickListener; 15 import android. widget. button; 16 import android. widget. editText; 17 18 public class ActivityDemo2 extends Activity implements OnClickListener {19 private EditText et_verify_empty; 20 private Button btn_verify; 21 @ Override22 protected void onCreate (Bundle savedInstanceState) {23 // TODO Auto-generated method stub24 super. onCreate (savedInstanceState); 25 setContentView (R. layout. activity02); 26 setTitle ("EditText achieves input restrictions and Verification"); 27 et_verify_empty = (EditText) findViewById (R. id. et_verify_empty); 28 btn_verify = (Button) findViewById (R. id. btn_verify); 29 btn_verify.setOnClickListener (this); 30} 31 @ Override32 public void onClick (View arg0) {33 // TODO Auto-generated method stub34 String s = et_verify_empty.getText (). toString (); 35 // if (s = null | s. length () = 0) {36 if (TextUtils. isEmpty (s) {37 et_verify_empty.setError ("cannot be blank"); 38}39} 40}
/EditTextDemo1/res/layout/activity02.xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 8 <TextView 9 android: layout_width = "match_parent" 10 android: layout_height = "wrap_content" 11 android: text = "via android: digits limit can only be entered in lower case abc "12/> 13 <EditText 14 android: id =" @ + id/et_limit_abc "15 android: layout_width =" match_parent "16 android: layout_height = "wrap_content" 17Android: digits = "abc"18/> 19 20 <TextView 21 android: layout_width = "match_parent" 22 android: layout_height = "wrap_content" 23 android: text = "via android: inputType is limited to only numbers "24/> 25 <! -- Various restrictions can be set in android: inputType, such as email addresses. --> 26 <EditText 27 android: id = "@ + id/et_limit_number" 28 android: layout_width = "match_parent" 29 android: layout_height = "wrap_content" 30Android: inputType = "number"31/> 32 33 <TextView 34 android: layout_width = "match_parent" 35 android: layout_height = "wrap_content" 36 android: text = "check whether EditText is empty by code" 37/> 38 <! -- Various restrictions can be set in android: inputType, such as email addresses. --> 39 <EditText 40 android: id = "@ + id/et_verify_empty" 41 android: layout_width = "match_parent" 42 android: layout_height = "wrap_content" 43 android: inputType = "" 44/> 45 <Button 46 android: id = "@ + id/btn_verify" 47 android: layout_width = "match_parent" 48 android: layout_height = "wrap_content" 49 android: text = "Start verification" 50/> 51 52 </LinearLayout>