首页 > 系统 > Android > 正文

EditText实现输入限制和校验功能实例代码

2019-12-12 02:18:47
字体:
来源:转载
供稿:网友

一、方法

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实现输入限制和校验功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表