• EditText实现输入限制和校验


    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>
  • 相关阅读:
    Redis 缓存 + Spring 的集成示例
    ETCD相关介绍--整体概念及原理方面
    SpringCloud微框架系列整体模块梳理
    win7如何修改磁盘驱动器号,怎么修改磁盘名称
    Android ListView中子控件的状态保存以及点击子控件改变子控件状态
    Android 自己动手写ListView学习其原理 3 ItemClick,ItemLongClick,View复用
    点击itemView选中checkbox
    Android-RecyclerView-Item点击事件设置
    onItemClickListener监听的整个item的点击。如何只监听那个framelayout的点击 onItemClickListener监听的整个item的点击。如何只监听那个framelayout的点击
    listView中setOnItemClickListener和getSelectedItemPosition()取不到position问题
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/7286752.html
Copyright © 2020-2023  润新知