如何在android中处理EditText Change Listener

时间:2011-10-25 11:34:26

标签: android android-edittext listener

想象一下,我有5个EditText,我需要在更改一个edittext值时填充值。价值观是相互依存的。我使用以下逻辑来实现这一目标。

//Finding which edit text is originating values 

for(int i=0;i<mEditTextIds.length;i++){ 
        txt[i]=(EditText) findViewById(mEditTextIds[i]);
        txt[i].setOnTouchListener(new EditTextTouchLisner());
        txt[i].setId(i);
}


//Enable that Edittext's TextChangeListner and disabling all other Edittext's TextChangeListner ..

if (event.getAction() == MotionEvent.ACTION_DOWN){
    for(int i=0;i<mTableRowIds.length;i++){
        if(i!=v.getId()){
            txt[i].addTextChangedListener(null);
        }       
    }
    txt[v.getId()].addTextChangedListener(new TextChangedListner());


//calculating values 

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    //calculate();
    Toast.makeText(getApplicationContext(), "CHANGED", Toast.LENGTH_SHORT) .show(); 
}

但如果我再次点击edittext,我会收到以下错误:

10-25 16:23:05.149: ERROR/AndroidRuntime(2378): FATAL EXCEPTION: main
10-25 16:23:05.149: ERROR/AndroidRuntime(2378): java.lang.NullPointerException
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.widget.TextView.sendBeforeTextChanged(TextView.java:6116)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.widget.TextView.access$700(TextView.java:187)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.widget.TextView$ChangeWatcher.beforeTextChanged(TextView.java:6309)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.sendTextWillChange(SpannableStringBuilder.java:878)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:279)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:384)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:292)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.os.Looper.loop(Looper.java:123)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at java.lang.reflect.Method.invoke(Method.java:521)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-25 16:23:05.149: ERROR/AndroidRuntime(2378):     at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:16)

您可以将 EditTextTouchLisner 实施到您的EditText,而不是实施 addTextChangedListener

addTextChangedListener将TextWatcher个对象作为参数

这是Help With TextWatcher

答案 1 :(得分:0)

如果使用焦点存储EditText,并且在更改侦听器内部,只有在生成更改事件的视图等于获得焦点的视图时才执行操作?

执行此操作,无需附加,分离和重新附加侦听器。

只是我的2美分......

答案 2 :(得分:0)

删除Textchangelistener

  

TXT [I] .removeTextChangedListener(观察者);

答案 3 :(得分:0)

我有同样的问题但在我的情况下有不同的原因:

我在初始化TextChangedListener之前将textChangedListener添加到EditText

所以我有这个:

filterText.addTextChangedListener(filterTextWatcher);
filterTextWatcher = new NavigationTextWatcher(adapter);
setListAdapter(adapter);

而不是:

filterTextWatcher = new NavigationTextWatcher(adapter);
filterText.addTextChangedListener(filterTextWatcher);
setListAdapter(adapter);