gpt4 book ai didi

android - 如何在android中处理EditText Change Listener

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:37 37 4
gpt4 key购买 nike

假设我有 5 个 EditText,我需要在更改一个 edittext 值时填充值。这些值(value)观是相互依存的。我正在使用以下逻辑来实现这一点。

//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)

最佳答案

除了实现 EditTextTouchLisner,您还可以对 EditText 实现 addTextChangedListener

addTextChangedListener 需要 TextWatcher对象作为参数

这是一个Help With TextWatcher

关于android - 如何在android中处理EditText Change Listener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7888638/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com