EditText实现输入限制和校验_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > EditText实现输入限制和校验

EditText实现输入限制和校验

 2017/8/4 21:31:17  饭饭_fan  程序员俱乐部  我要评论(0)
  • 摘要:EditText实现输入限制和校验一、方法1)输入限制1、通过android:digits限制只能输入小写abcandroid:digits="abc"2、通过android:inputType限制只能输入数字android:inputType="number"在android:inputType中可以设置各种限制,比如邮箱地址等等2)校验直接通过代码实现Strings=et_verify_empty.getText().toString();if(s==null||s.length()==0
  • 标签:实现 限制

EditText实现输入限制和校验

一、方法

1)输入限制

1、通过android:digits限制只能输入小写abc

android:digits="abc"

 

2、通过android:inputType限制只能输入数字

android:inputType="number"

在android:inputType中可以设置各种限制,比如邮箱地址等等

 

2)校验

直接通过代码实现

String s=et_verify_empty.getText().toString();
if(s==null||s.length()==0){
  et_verify_empty.setError("不能为空");
}

 

二、代码实例

效果图

代码

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     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         // TODO Auto-generated method stub
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity02);
26         setTitle("EditText实现输入限制和校验");
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     @Override
32     public void onClick(View arg0) {
33         // TODO Auto-generated method stub
34         String s=et_verify_empty.getText().toString();
35         //if(s==null||s.length()==0){
36         if(TextUtils.isEmpty(s)){
37             et_verify_empty.setError("不能为空");
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="通过android:digits限制只能输入小写abc"
12         />
13     <EditText 
14         android:id="@+id/et_limit_abc"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:digits="abc"
18         />
19     
20      <TextView 
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="通过android:inputType限制只能输入数字"
24         />
25      <!-- 在android:inputType中可以设置各种限制,比如邮箱地址等等 -->
26     <EditText 
27         android:id="@+id/et_limit_number"
28         android:layout_width="match_parent"
29         android:layout_height="wrap_content"
30         android:inputType="number"
31         />
32     
33     <TextView 
34         android:layout_width="match_parent"
35         android:layout_height="wrap_content"
36         android:text="通过代码校验EditText是否为空"
37         />
38      <!-- 在android:inputType中可以设置各种限制,比如邮箱地址等等 -->
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="开始校验"
50         />
51 
52 </LinearLayout>

 

发表评论
用户名: 匿名