和我在南方一起工作的朋友说,“南北方的差异其实蛮大的。”我家在北方,也在南方工作,不过我倒是觉得差异不怎么大,因为我在北方的时候,就没有女朋友,而来到了南方,同样没有女朋友。

开发时遇到一个问题,如同标题,当一个类继承了TextWatcher时,倘若这个类中有很多EditText控件,那么如何知道调用TextWatcher的是哪一个EditText控件呢?如果一个类继承的是OnClickListener,那可以通过View获取控件的Id值,从而分辨控件,做对应操作。可惜TextWatcher似乎没有类似的方法。我是这样解决的:

布局文件:

<LinearLayoutxmlns: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:orientation="vertical"android:padding="10dp"><EditTextandroid:id="@+id/edit1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/edittext_1"/><EditTextandroid:id="@+id/edit2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/edittext_2"/><EditTextandroid:id="@+id/edit3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/edittext_3"/><EditTextandroid:id="@+id/edit4"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/edittext_4"/><EditTextandroid:id="@+id/edit5"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/edittext_5"/></LinearLayout>

很简单,只是写了几个EditText控件。

主类:

packagecom.example.edittexttest;importandroid.app.Activity;importandroid.os.Bundle;importandroid.text.Editable;importandroid.text.TextWatcher;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.TextView;importandroid.widget.Toast;publicclassEditTextTestextendsActivity{EditTextedit1,edit2,edit3,edit4,edit5;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_edit_text_test);edit1=(EditText)findViewById(R.id.edit1);edit1.addTextChangedListener(newClassOfTextWatcher(edit1));setCursorToEnd(edit1);edit2=(EditText)findViewById(R.id.edit2);edit2.addTextChangedListener(newClassOfTextWatcher(edit2));setCursorToEnd(edit2);edit3=(EditText)findViewById(R.id.edit3);edit3.addTextChangedListener(newClassOfTextWatcher(edit3));setCursorToEnd(edit3);edit4=(EditText)findViewById(R.id.edit4);edit4.addTextChangedListener(newClassOfTextWatcher(edit4));setCursorToEnd(edit4);edit5=(EditText)findViewById(R.id.edit5);edit5.addTextChangedListener(newClassOfTextWatcher(edit5));setCursorToEnd(edit5);}privateclassClassOfTextWatcherimplementsTextWatcher{privateTextViewview;publicClassOfTextWatcher(Viewview){if(viewinstanceofTextView)this.view=(TextView)view;elsethrownewClassCastException("viewmustbeaninstanceOfTextView");}@OverridepublicvoidafterTextChanged(Editables){if(s.length()<=0){switch(view.getId()){caseR.id.edit1:Toast.makeText(EditTextTest.this,"第一个编辑框为空!",Toast.LENGTH_LONG).show();break;caseR.id.edit2:Toast.makeText(EditTextTest.this,"第二个编辑框为空!",Toast.LENGTH_LONG).show();break;caseR.id.edit3:Toast.makeText(EditTextTest.this,"第三个编辑框为空!",Toast.LENGTH_LONG).show();break;caseR.id.edit4:Toast.makeText(EditTextTest.this,"第四个编辑框为空!",Toast.LENGTH_LONG).show();break;caseR.id.edit5:Toast.makeText(EditTextTest.this,"第五个编辑框为空!",Toast.LENGTH_LONG).show();break;default:break;}}}@OverridepublicvoidbeforeTextChanged(CharSequences,intstart,intcount,intafter){}@OverridepublicvoidonTextChanged(CharSequences,intstart,intbefore,intcount){}}//将编辑框的光标移动到末尾publicvoidsetCursorToEnd(EditTexttext){Stringcontent=text.getText().toString();text.setSelection(content.length());}}

写了一个叫“ClassOfTextWatcher”的内部类,它实现了TextWatcher接口,这个内部类的构造方法中传入View控件来获取控件的Id。

效果图: