TextWatcherはEditTextの文字入力を監視します。例えば入力パスワードの制限文字数がある場合に「文字数オーバー」と表示を出すなどに使えます。
2021.1.1
TextWatcher
スマホでは特にユーザーが文字入力を確定しないまま次に行ってしまうケースがあります。あるいは確定ボタンを入れるのがレイアウト上難しい場合など。
文字を監視して「入力が終わった」とか「文字数が越えている」などはEditText にTextWatcherを実装することで実現できます。
 
TextWatcherのポイント
TextWatcherの設定方法を見ていきます。
| 1 2 3 4 5 6 7 8 | // TextWatherのインポート import android.text.TextWatcher; //TextWatcherの実装 public class MainActivity extends Activity implements TextWatcher{ //リスナーを登録 addTextChangedListener(this) | 
 
implements TextWatcher とすると以下のメソッドが呼び出されます。
| 1 2 3 4 5 6 7 8 9 10 11 | @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) { } | 
beforeTextChanged(CharSequence s, int start, int count,int after)
- 文字列が修正される直前に呼び出されるメソッド
- CharSequence s
- 現在EditTextに入力されている文字列
 
- int start
- sの文字列で新たに追加される文字列のスタート位置
 
- int count
- sの文字列の中で変更された文字列の総数
 
- int after
- 新規に追加された文字列の数
 
onTextChanged(CharSequence s, int start, int before, int count)
- 文字1つを入力した時に呼び出される
- CharSequence s
- 現在EditTextに入力されている文字列
 
- int start
- sの文字列で新たに追加される文字列のスタート位置
 
- int before
- 削除される既存文字列の数
 
- int count
- 新たに追加された文字列の数
 
afterTextChanged(Editable s)
- 最後にこのメソッドが呼び出される
- Editable s
- 最終的にできた修正可能な、変更された文字列
 
リアルタイムで文字入力の前、最中、後と設定ができるわけです。
サンプルコード
実際にコードを記述すると
 
MainActivity.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | //package your.package.name; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements TextWatcher {     private TextView textView;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         EditText editText = findViewById(R.id.edit_text);         // リスナーを登録         editText.addTextChangedListener(this);         textView = findViewById(R.id.text_view);     }     @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) {         // テキスト変更後に変更されたテキストを取り出す         String inputStr= s.toString();         // 文字長をカウントして8文字を超えると「オーバー」とする         if(inputStr.length() >8){             String str = inputStr +" 文字数オーバー";             textView.setText(str);             textView.setTextColor(Color.RED);         }         else{             textView.setText(inputStr);             textView.setTextColor(Color.BLACK);         }     } } | 
 
レイアウトです
activity_main.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#dfe"     android:orientation="vertical"     android:padding="20dp"     tools:context=".MainActivity" >     <TextView         android:id="@+id/text_view"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margin="20dp"         android:textSize="20sp"         android:gravity="start" />     <EditText         android:id="@+id/edit_text"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margin="20dp"         android:background="#ffffff"         android:textSize="30sp"         android:hint="@string/hint"         android:inputType="text"         android:importantForAutofill="no"/> </LinearLayout> | 
 
android:importantForAutofill=”no”
はautofillHintsの設定が無い場合のワーニングを消すためです
 
リソース
strings.xml
| 1 2 3 4 | <resources>     <string name="app_name">Your App Name</string>     <string name="hint">8文字まで入力可</string> </resources> | 
サンプル動画
テキストの文字入力数をカウントする程度であれば afterTextChanged(Editable s) を使うのが一番簡単です。他は細かくやりたい場合でしょうか。
 
関連ページ:
- スマホにEditTextを使って文字を入力する
- EditTextをコードで記述する
- 文字入力と表示制限
- TextWatcher 入力を監視する
Reference:
TextWatcher| Android Developers
![[Android] 入力を監視するTextWatcher 1x1.trans - [Android] 入力を監視するTextWatcher](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
