如何在 Android 中的 EditText 更改監聽器中計數字符?


在某些情況下,我們需要限制 EditText 中某些字元的輸入。為了解決這種情況,本示例演示瞭如何在 EditText 更改監聽器中計數字符。

步驟 1 - 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必要的資訊以建立新專案。

步驟 2 - 將以下程式碼新增到 res/layout/activity_main.xml 中。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:gravity = "center"
   android:background = "#33FFFF00"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/text"
      android:textSize = "18sp"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
</LinearLayout>

在上面的程式碼中,我們使用了 EditText。它將檢查輸入字元的長度,如果超過 5 個,則顯示錯誤訊息。

步驟 3 - 將以下程式碼新增到 src/MainActivity.java 中

package com.example.andy.myapplication;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   EditText text;
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      text = findViewById(R.id.text);
      text.addTextChangedListener(new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) { }
         @Override
         public void afterTextChanged(Editable s) {
            if(s.toString().length()>5) {
               text.setError("It allows only 5 character");
            }else{
               text.setError(null);
            }
         }
      });
   }
}

在上面的程式碼中,我們使用了文字更改監聽器,在文字更改後,我們驗證文字,如下所示 -

text.addTextChangedListener(new TextWatcher() {
   @Override
   public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) { }
   @Override
   public void afterTextChanged(Editable s) {
      if(s.toString().length()>5) {
         text.setError("It allows only 5 character");
      }else{
         text.setError(null);
      }
}
});

讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的電腦。要從 Android Studio 執行應用程式,請開啟您的專案中的一個活動檔案,然後單擊工具欄中的執行  圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

當您輸入 5 個字元時,它不會顯示任何錯誤。如果您輸入超過 5 個字元,它將顯示如下所示的錯誤 -

點選 這裡 下載專案程式碼

更新於: 2019-07-30

954 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.