简体   繁体   English

根据另一个EditText更改EditText

[英]Change EditText according to another EditText

I'm new to Android development. 我是Android开发的新手。 I have two EditTexts, and I'm trying to change value of ET1 according to ET2, and also value of ET2 according to ET1, automatically during the typing. 我有两个EditText,并且尝试在键入期间自动根据ET2更改ET1的值,以及根据ET1更改ET2的值。

Sum of their values must be 200. So if I want to type 56 in ET1, when I type 5, ET2 should display 195. When I add 6, ET2 should display 144 (200 - 56). 它们的值之和必须为200。因此,如果我想在ET1中键入56,则当我键入5时,ET2应该显示195。当我添加6时,ET2应该显示144(200-56)。 Everything should happen while focus is still on ET1. 当焦点仍然放在ET1上时,一切都会发生。

scoreWe.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (v.hasFocus()) {
                scoreYou.addTextChangedListener(null);
                scoreWe.addTextChangedListener(new TextWatcher() {
                    @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) {
                        int scoreW;
                        try {
                            scoreW = Integer.parseInt(scoreWe.getText().toString());
                        } catch (NumberFormatException e) {
                            scoreW = 0;
                        }
                        int scoreY = 200 - scoreW;
                        String scY = Integer.toString(scoreY);
                        scoreYou.setText(scY);
                    }
                });
            }
        }
    });

- -

scoreYou.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (v.hasFocus()) {
                scoreWe.addTextChangedListener(null);
                scoreYou.addTextChangedListener(new TextWatcher() {
                    @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) {
                        int scoreY;
                        try {
                            scoreY = Integer.parseInt(scoreYou.getText().toString());
                        } catch (NumberFormatException e) {
                            scoreY = 0;
                        }
                        int scoreW = 200 - scoreY;
                        String scW = Integer.toString(scoreW);
                        scoreWe.setText(scW);
                    }
                });
            }
        }
    });

Those are 2 OnFocusChangeListeners, and after I try to input something in any of the EditTexts, app crashes instantly. 那是2个OnFocusChangeListeners,在我尝试在任何EditText中输入内容后,应用程序立即崩溃。 Log: 日志:

09-18 16:50:22.362 17764-17764/com.example.dvido.bela E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.example.dvido.bela, PID: 17764
                                                                    java.lang.NullPointerException: Attempt to invoke interface method 'void android.text.TextWatcher.beforeTextChanged(java.lang.CharSequence, int, int, int)' on a null object reference
                                                                        at android.widget.TextView.sendBeforeTextChanged(TextView.java:7941)
                                                                        at android.widget.TextView.setText(TextView.java:4242)
                                                                        at android.widget.TextView.setText(TextView.java:4199)
                                                                        at android.widget.EditText.setText(EditText.java:84)
                                                                        at android.widget.TextView.setText(TextView.java:4174)
                                                                        at com.example.dvido.bela.InputGame$1$1.afterTextChanged(InputGame.java:89)
                                                                        at android.widget.TextView.sendAfterTextChanged(TextView.java:8007)
                                                                        at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10168)
                                                                        at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1043)
                                                                        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:560)
                                                                        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:492)
                                                                        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:491)
                                                                        at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
                                                                        at android.widget.TextView.doKeyDown(TextView.java:6098)
                                                                        at android.widget.TextView.onKeyDown(TextView.java:5911)
                                                                        at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
                                                                        at android.view.View.dispatchKeyEvent(View.java:9234)
                                                                        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
                                                                        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
                                                                        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
                                                                        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
                                                                        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
                                                                        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
                                                                        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1640)
                                                                        at com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2395)
                                                                        at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1727)
                                                                        at android.app.Activity.dispatchKeyEvent(Activity.java:2725)
                                                                        at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:543)
                                                                        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
                                                                        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:312)
                                                                        at com.android.internal.policy.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2310)
                                                                        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4127)
                                                                        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4089)
                                                                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                                                                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                                                                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                                                                        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
                                                                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                                                                        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
                                                                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                                                                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                                                                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                                                                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                                                                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                                                                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                                                                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                                                                        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3820)
                                                                        at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3981)
                                                                        at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2253)
                                                                        at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1874)

09-18 16:50:36.917 17764-17764/com.example.dvido.bela I/Process: Sending signal. 09-18 16:50:36.917 17764-17764 / com.example.dvido.bela I / Process:正在发送信号。 PID: 17764 SIG: 9 PID:17764 SIG:9

My best guess is due to the text changed listeners are getting called on both edittext views at same time. 我最好的猜测是由于在两个edittext视图上同时调用了已更改文本的侦听器。

Try disabling the Text changed listener of ET1 on focus of ET2 and vice-versa. 尝试在ET2的焦点上禁用ET1的Text changed listener ,反之亦然。 Do something like this 做这样的事情

Disable scoreWe textChanged Listener 禁用分数我们textChanged侦听器

scoreYou.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (v.hasFocus()) {
             scoreWe.addTextChangedListener(null); // ADD THIS LINE
             scoreYou.addTextChangedListener(new TextWatcher(){}); // Add and Implement this line also
             //do rest of your logic
        }
    }
});

Disable scoreYou textChanged Listener 禁用分数您textChanged侦听器

scoreWe.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (v.hasFocus()) {
             scoreYou.addTextChangedListener(null); // ADD THIS LINE
             scoreYou.addTextChangedListener(new TextWatcher(){}); // Add and Implement this line also
             //do rest of your logic
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM