
android EditText で入力されたEditTextを識別するサンプル@tryTextWatcherExtend00


android EditText で入力されたEditTextを識別する
文字が変更された時、変更を検知する。
大事なところ
TextWatcherを拡張したものでEditTextのIdをつけて
呼び出すように改造する
EditText→TextWatcher→Activityの順番に
呼び出されるようにすることで、処理を分割する。
関連項目
EditText
android.text.TextWatcher
addTextChangedListener
開発環境
Eclipse IDE バージョン: 3.7 Indigo Service Release 1
ターゲットプラットフォーム: 2.1
API レベル: 7
package trial.sample.trytextwatcherextend00; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; public class TryTextWatcherExtend00Activity extends Activity // ↓コールバックをActivityに定義するようにする implements ITextWatcherCallback { private EditText mInput1Edit; // 入力① private EditText mOutput1Edit; // 表示されるところ① private EditText mInput2Edit; // 入力② private EditText mOutput2Edit; // 表示されるところ② /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // エディットボックスへのアクセスの準備 this.mInput1Edit = (EditText)this.findViewById(R.id.editText1); this.mInput1Edit.addTextChangedListener(new TextWatcherExtend(R.id.editText1, this)); this.mOutput1Edit = (EditText)this.findViewById(R.id.editText2); this.mInput2Edit = (EditText)this.findViewById(R.id.editText3); this.mInput2Edit.addTextChangedListener(new TextWatcherExtend(R.id.editText3, this)); this.mOutput2Edit = (EditText)this.findViewById(R.id.editText4); } //////////////////////////////////////////////////////////// // ここから下の関数に入力ボックスの処理がすべて集まる //////////////////////////////////////////////////////////// // テキスト変更後 public void afterTextChanged(int editTextId, Editable s) { } //////////////////////////////////////////////////////////// // テキスト変更前 public void beforeTextChanged(int editTextId, CharSequence s, int start, int count, int after) { // TODO 自動生成されたメソッド・スタブ } //////////////////////////////////////////////////////////// // テキスト変更後 public void onTextChanged(int editTextId, CharSequence s, int start, int before, int count) { switch (editTextId) { // 入力①ボックスの値に変化があった時は case R.id.editText1: // 出力①の場所に表示 this.mOutput1Edit.setText(s.toString()); break; // 入力②ボックスの値に変化があった case R.id.editText3: // 出力②の場所に表示 this.mOutput2Edit.setText(s.toString()); break; default: break; } } } //////////////////////////////////////////////////////////// // TextWatcherが呼ばれたときにこれが呼び出されるようにする // editTextIdでどのEditTextが変更されたか判別する interface ITextWatcherCallback { public void afterTextChanged(int editTextId, Editable s); public void beforeTextChanged(int editTextId, CharSequence s, int start, int count, int after); public void onTextChanged(int editTextId, CharSequence s, int start, int before, int count); } //////////////////////////////////////////////////////////// // テキストの変更を監視 class TextWatcherExtend implements TextWatcher { final int mEditTextId; final ITextWatcherCallback mCallback; //////////////////////////////////////////////////////////// // コンストラクタ public TextWatcherExtend(final int editTextId, final ITextWatcherCallback callback) { this.mEditTextId = editTextId; this.mCallback = callback; } //////////////////////////////////////////////////////////// // テキスト変更後 public void afterTextChanged(Editable s) { // EditTextのIDをくっつけて呼び出し this.mCallback.afterTextChanged(this.mEditTextId, s); } //////////////////////////////////////////////////////////// // テキスト変更前 public void beforeTextChanged(CharSequence s, int start, int count, int after) { // EditTextのIDをくっつけて呼び出し this.mCallback.beforeTextChanged(this.mEditTextId, s, start, count, after); } //////////////////////////////////////////////////////////// // テキスト変更後 public void onTextChanged(CharSequence s, int start, int before, int count) { // EditTextのIDをくっつけて呼び出し this.mCallback.onTextChanged(this.mEditTextId, s, start, before, count); } }
android EditText 文字が変更された時、変更を検知するサンプル@tryEditTe... AndroidのSurfaceViewの上にコントロール(Button,EditText)を配置するサン...