EditText實(shí)現(xiàn)輸入限制和校驗(yàn)功能實(shí)例代碼
一、方法
1)輸入限制
1、通過android:digits限制只能輸入小寫abc
android:digits="abc"
2、通過android:inputType限制只能輸入數(shù)字
android:inputType="number"
在android:inputType中可以設(shè)置各種限制,比如郵箱地址等等
2)校驗(yàn)
直接通過代碼實(shí)現(xiàn)
String s=et_verify_empty.getText().toString();
if(s==null||s.length()==0){
et_verify_empty.setError("不能為空");
}
二、代碼實(shí)例
效果圖

代碼
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實(shí)現(xiàn)輸入限制和校驗(yàn)");
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限制只能輸入數(shù)字"
/>
<!-- 在android:inputType中可以設(shè)置各種限制,比如郵箱地址等等 -->
<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="通過代碼校驗(yàn)EditText是否為空"
/>
<!-- 在android:inputType中可以設(shè)置各種限制,比如郵箱地址等等 -->
<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="開始校驗(yàn)"
/>
</LinearLayout>
總結(jié)
以上所述是小編給大家介紹的EditText實(shí)現(xiàn)輸入限制和校驗(yàn)功能,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Android斷點(diǎn)續(xù)傳下載器JarvisDownloader的示例
本篇文章主要介紹了Android斷點(diǎn)續(xù)傳下載器JarvisDownloader的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android開發(fā):微信授權(quán)登錄與微信分享完全解析
本篇文章主要介紹了Android微信授權(quán)登錄與微信分享,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
Android Studio Gradle插件版本與Gradle版本之間的對(duì)應(yīng)關(guān)系
今天小編就為大家分享一篇關(guān)于Android Studio Gradle插件版本與Gradle版本之間的對(duì)應(yīng)關(guān)系,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android分頁中顯示出下面翻頁的導(dǎo)航欄的布局實(shí)例代碼
這篇文章主要介紹了Android分頁中顯示出下面翻頁的導(dǎo)航欄的布局實(shí)例代碼,需要的朋友可以參考下2017-04-04
android 中win10 使用uwp控件實(shí)現(xiàn)進(jìn)度條Marquez效果
這篇文章主要介紹了android 中win10 使用uwp控件實(shí)現(xiàn)進(jìn)度條Marquez效果,需要的朋友可以參考下2017-06-06
Android Fragment滑動(dòng)組件ViewPager的實(shí)例詳解
這篇文章主要介紹了Android Fragment滑動(dòng)組件ViewPager的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05

