Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để Đếm ký tự trong Trình nghe đã thay đổi EditText trong Android?

Một số tình huống, chúng tôi phải hạn chế một văn bản chỉnh sửa cho một số ký tự. Để giải quyết tình huống này, trong ví dụ này trình bày cách đếm các ký tự trong trình nghe chỉnh sửa văn bản đã thay đổi.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "https://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>

Trong đoạn mã trên, chúng tôi đã sử dụng một văn bản chỉnh sửa. Nó sẽ kiểm tra độ dài của các ký tự đã nhập, nếu vượt quá 5 thì nó sẽ hiển thị thông báo lỗi.

Bước 3 - Thêm mã sau vào 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);
            }
         }
      });
   }
}

Trong đoạn mã trên, chúng tôi đã sử dụng trình nghe đã thay đổi văn bản, sau khi văn bản được thay đổi, chúng tôi sẽ xác thực văn bản như được hiển thị bên dưới -

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);
      }
}
});

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để Đếm ký tự trong Trình nghe đã thay đổi EditText trong Android?

Khi bạn nhập 5 ký tự, nó không hiển thị bất kỳ lỗi nào. nếu bạn nhập nhiều hơn 5 ký tự. nó sẽ hiển thị lỗi như hình dưới đây -

Làm thế nào để Đếm ký tự trong Trình nghe đã thay đổi EditText trong Android?