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

 package fry;

 import com.example.editTextDemo1.R;

 import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class ActivityDemo2 extends Activity implements OnClickListener{
private EditText et_verify_empty;
private Button btn_verify;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
setTitle("EditText实现输入限制和校验");
et_verify_empty=(EditText) findViewById(R.id.et_verify_empty);
btn_verify=(Button) findViewById(R.id.btn_verify);
btn_verify.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// 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 }
}
}

/editTextDemo1/res/layout/activity02.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过android:digits限制只能输入小写abc"
/>
<EditText
android:id="@+id/et_limit_abc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abc"
/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过android:inputType限制只能输入数字"
/>
<!-- 在android:inputType中可以设置各种限制,比如邮箱地址等等 -->
<EditText
android:id="@+id/et_limit_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过代码校验EditText是否为空"
/>
<!-- 在android:inputType中可以设置各种限制,比如邮箱地址等等 -->
<EditText
android:id="@+id/et_verify_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
/>
<Button
android:id="@+id/btn_verify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始校验"
/> </LinearLayout>

EditText实现输入限制和校验的更多相关文章

  1. Android限定EditText的输入类型为数字或者英文(包括大小写),EditText,TextView只能输入两位小数

    Android限定EditText的输入类型为数字或者英文(包括大小写) // 监听密码输入框的输入内容类型,不可以输入中文    TextWatcher mTextWatcher = new Tex ...

  2. 限制EditText只能输入小数点后两位

    设置EditText只能输入小数点后两位,在价格等有限制的输入时特别有效 TextWatcher textWatcher = new TextWatcher() { @Override public ...

  3. Android 开发之拦截EditText的输入内容,定制输入内容

    1.EditText作为一个比较成熟的View,在Android的应用开发中得到极为广泛的使用.在某些特殊情况下,我们可能需要定制EditText的输入内容, 只允许指定功能的输入,例如输入一个”dd ...

  4. 【转】android 中如何限制 EditText 最大输入字符数

    原文网址:http://blog.csdn.net/fulinwsuafcie/article/details/7437768 方法一: 在 xml 文件中设置文本编辑框属性作字符数限制 如:andr ...

  5. android 中如何限制 EditText 最大输入字符数

    方法一: 在 xml 文件中设置文本编辑框属性作字符数限制 如:android:maxLength="10" 即限制最大输入字符个数为10 方法二: 在代码中使用InputFilt ...

  6. 如何获取一个AlertDialog中的EditText中输入的内容

    怎么获取一个AlertDialog中的EditText中输入的内容? new AlertDialog.Builder(this)   .setTitle("请输入")   .set ...

  7. android代码集EditText只要输入号码、信

     如何设置EditText,因此,只有某些数字或字母可以进入它? 一.建立EditText,只要输入号码: 办法1:直接生成DigitsKeyListener了. et_1.setKeyListe ...

  8. Android中EditText设置输入条件

    一.应用场景 之前做商城应用时,会有对用户资料的设置情况进行限制,如下: (1)用户邮箱,应当只允许输入英文字母,数字和@.两个符号, (2)用户手机,应当只能输入数字,禁止输入其他字符. (3)用户 ...

  9. AlertDialog中的EditText不能输入

    一.描述 在项目中有碰到使用AlertDialog,给他设置自定义布局,自定义布局中有包含EditText,但是运行起来后发现EditText不能输入文字,没有焦点,一开始还以为是事件拦截掉了,后来试 ...

随机推荐

  1. Powershell Get Domain Group的几种方法

    Group常见属性介绍: 一.Get-ADGroup获取群组(如下例循环获取群组的发送权限) #群组的发送权限info $groups=Get-ADGroup -filter * -SearchSco ...

  2. Storm-源码分析汇总

    Storm Features Storm 简介 Storm Topology的并发度 Storm - Guaranteeing message processing Storm - Transacti ...

  3. centos7 docker镜像加速器配置

    CentOS的配置方式略微复杂,需要先将默认的配置文件复制出来 /lib/systemd/system/docker.service -> /etc/systemd/system/docker. ...

  4. c++编译/连接/运行

    1.gcc命令&makefile语法&makefile编写: https://www.cnblogs.com/ycloneal/p/5230266.html 2.头文件&库文件 ...

  5. 4.Data Types in the mongo Shell-官方文档摘录

    总结: 1.MongoDB 的BSON格式支持额外的数据类型 2 Date 对象内部存储64位字节存整数,存储使用NumberLong()这个类来存,使用NumberInt()存32位整数,128位十 ...

  6. jvm堆配置参数

    1.-Xms初始堆大小默认物理内存的1/64(<1GB)(官方建议)2.-Xmx最大堆大小默认物理内存的1/4(<1GB)(官方建议),实际中建议不大于4GB3.一般建议设置 -Xms=- ...

  7. Spark SQL原理和实现--王家林老师

  8. python2中range和xrange的区别

    range和xrange用法相同,不同的是xrange不是生成一个序列,而是作为一个生成器,即生成一个取出一个 相对来说,xrange比range性能优化很多,因为不需要一下子开辟一块很大的内存,特别 ...

  9. JavaScript:关闭当前页面(微信、电脑)

    WeixinJSBridge.call('closeWindow'); // 关闭微信浏览器 window.opener=null; window.open('','_self'); window.c ...

  10. Spring框架学习之IOC(二)

    Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...