这篇文章主要介绍了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
String s=et_verify_empty.getText().toString();
//if(s==null||s.length()==0){
if(TextUtils.isEmpty(s)){
et_verify_empty.setError("不能为空");
}
}
}
/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如何实现输入限制和校验功能”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。