EditText實(shí)現(xiàn)輸入限制和校驗(yàn)功能實(shí)例代碼
一、方法
1)輸入限制
1、通過(guò)android:digits限制只能輸入小寫abc
android:digits="abc"
2、通過(guò)android:inputType限制只能輸入數(shù)字
android:inputType="number"
在android:inputType中可以設(shè)置各種限制,比如郵箱地址等等
2)校驗(yàn)
直接通過(guò)代碼實(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="通過(guò)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="通過(guò)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="通過(guò)代碼校驗(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="開(kāi)始校驗(yàn)"
/>
</LinearLayout>
總結(jié)
以上所述是小編給大家介紹的EditText實(shí)現(xiàn)輸入限制和校驗(yàn)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Android編程實(shí)現(xiàn)攝像頭臨摹效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)攝像頭臨摹效果的方法,涉及Android權(quán)限控制、布局及攝像頭功能調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Android編程中TextView寬度過(guò)大導(dǎo)致Drawable無(wú)法居中問(wèn)題解決方法
這篇文章主要介紹了Android編程中TextView寬度過(guò)大導(dǎo)致Drawable無(wú)法居中問(wèn)題解決方法,以實(shí)例形式較為詳細(xì)的分析了TextView設(shè)置及xml布局與調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android實(shí)現(xiàn)多線程下載文件的方法
這篇文章主要介紹了Android實(shí)現(xiàn)多線程下載文件的方法,以實(shí)例形式較為詳細(xì)的分析了Android多線程文件傳輸及合并等操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android Studio 引入 aidl 文件的方法匯總
本文給大家分享的是在Android Studio中引入AIDL文件常用的兩種方法,小伙伴們根據(jù)自己的情況自由選擇,希望對(duì)大家能夠有所幫助2017-11-11
詳解Android Studio安裝ButterKnife插件(手動(dòng)安裝)
這篇文章主要介紹了詳解AndroidStudio安裝ButterKnife插件(手動(dòng)安裝),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android開(kāi)發(fā)筆記之:Dialog的使用詳解
本篇文章是對(duì)Android中Dialog的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
android?scrollview頂部漸漸消失實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了android?scrollview頂部漸漸消失實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

