源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-10-25 00:50 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:EditText实现输入限制和校验功能实例代码
[b]一、方法[/b] 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("不能为空");
 }
[b]二、代码实例[/b] 效果图 [img]http://files.jb51.net/file_images/article/201708/201708060857001.png[/img] 代码 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实现输入限制和校验功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部