国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > 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實現輸入限制和校驗功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金塔县| 甘谷县| 同心县| 府谷县| 九江县| 成都市| 宁津县| 什邡市| 绥棱县| 北辰区| 华亭县| 邵阳市| 灵石县| 托克托县| 鹤壁市| 犍为县| 紫金县| 云龙县| 苍溪县| 长岭县| 昭苏县| 荣成市| 喀什市| 奉新县| 阿荣旗| 鲁山县| 庆云县| 大庆市| 通山县| 湖州市| 临湘市| 盱眙县| 邵武市| 紫金县| 鄂托克前旗| 时尚| 汤原县| 南雄市| 那曲县| 讷河市| 新余市|