2016-09-14 102 views
2

我是Android開發新手。我有兩個EditText,我試圖在輸入時自動根據ET2更改ET1的值,並根據ET1自動更改ET2的值。根據另一個EditText更改EditText

它們的總和必須是200.所以如果我想在ET1中鍵入56,當我輸入5時,ET2應該顯示195.當我加6時,ET2應該顯示144(200-56)。一切都應該發生,而重點仍然放在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); 
        } 
       }); 
      } 
     } 
    }); 

這些都是2個OnFocusChangeListeners,以後我試着輸入一些在任何EditTexts的,應用立即崩潰。 日誌:

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 /過程:發送信號。 PID:17764 SIG:9

+0

那麼你的logcat的說,當你的主線程/應用程序被阻止? –

+0

你是什麼意思被封鎖?應用程序未響應?或墜毀? – SripadRaj

回答

0

我最好的猜測是由於文本更改的監聽器在同時調用兩個edittext視圖。

嘗試在ET2的焦點上禁用ET1的Text changed listener,反之亦然。做這樣的事情

禁用scoreWe框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 
     } 
    } 
}); 

禁用scoreYou框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 
     } 
    } 
}); 
+0

它的工作,但只是第一次。如果我將光標移動到ET2然後回到ET1,文本將不會更改,因爲偵聽器被禁用,所以我必須重新設置它。 – Coelacanth

+0

@Coelacanth我添加了代碼。請檢查更新後的答案。將其他監聽器設置爲空後,您必須重新設置監聽器。那會做這項工作。試着讓我知道。 – SripadRaj

+0

我已經改變了我的代碼,但仍然沒有。我已經在上面添加了它。感謝您的幫助。 – Coelacanth