EditText如何實(shí)現(xiàn)輸入限制和校驗(yàn)功能-創(chuàng)新互聯(lián)

這篇文章主要介紹了EditText如何實(shí)現(xiàn)輸入限制和校驗(yàn)功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比田東網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式田東網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋田東地區(qū)。費(fèi)用合理售后完善,十年實(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í)例

效果圖

EditText如何實(shí)現(xiàn)輸入限制和校驗(yàn)功能

代碼

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>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“EditText如何實(shí)現(xiàn)輸入限制和校驗(yàn)功能”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

網(wǎng)頁(yè)名稱:EditText如何實(shí)現(xiàn)輸入限制和校驗(yàn)功能-創(chuàng)新互聯(lián)
鏈接地址:http://muchs.cn/article14/psdde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、域名注冊(cè)自適應(yīng)網(wǎng)站、App設(shè)計(jì)關(guān)鍵詞優(yōu)化、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)