2011-10-08 54 views
0

我在佈局中有兩個EditText和一個TextView。我想改變EditText和TextView相對於另一個EditText值的值。和另一個editText相同。我已經實現了下面的代碼。無法爲兩個不同的EditText放置兩個TextWatcher監聽器?

amountText.setText(""); 
    excludedText.setText(""); 
    includedText.setText(""); 


    includedText.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

       if(!(includedText.getText().toString().equals(""))) 
       { 
        double included = Double.parseDouble(includedText.getText().toString()); 
        included = roundTwoDecimals(included); 
     //   double amountValue = (excluded*15/100); 

        String amt = String.valueOf(roundTwoDecimals(included-(included/1.15))); 
        String excluded = String.valueOf(included/1.15); 
        System.out.println("The Amount is: "+amt); 
        amountText.setText(amt); 
        try{ 
         excludedText.setText(""); 
         excludedText.setText(excluded); //////// Error 
        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 
       else 
       { 
        amountText.setText(""); 
        //excludedText.setText(""); 
       } 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    excludedText.addTextChangedListener(new TextWatcher() 
    { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      if(!(excludedText.getText().toString().equals(""))) 
      { 
       double excluded = Double.parseDouble(excludedText.getText().toString()); 
       excluded = roundTwoDecimals(excluded); 
     //  double amountValue = (excluded*15/100); 

       String amt = String.valueOf(roundTwoDecimals(excluded*0.15)); 
       String included = String.valueOf(roundTwoDecimals(excluded+(excluded*0.15))); 
       System.out.println("The Amount is: "+amt); 
       amountText.setText(amt); 
       includedText.setText(included); 
      } 
      else 
      { 
       amountText.setText(""); 
       includedText.setText(""); 
      } 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 
    }); 

可是當我運行應用程序,我得到了像計算器的錯誤。

錯誤的logcat是:

10-08 10:46:19.149: ERROR/AndroidRuntime(695): FATAL EXCEPTION: main 
10-08 10:46:19.149: ERROR/AndroidRuntime(695): java.lang.StackOverflowError 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.text.DynamicLayout.reflow(DynamicLayout.java:261) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.text.DynamicLayout.<init>(DynamicLayout.java:150) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.makeNewLayout(TextView.java:4851) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.checkForRelayout(TextView.java:5348) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2688) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:66) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:67) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:67) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:67) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$1.onTextChanged(GSTActivity.java:67) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2556) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.EditText.setText(EditText.java:75) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2531) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at com.project.TaxCalculator.GSTActivity$2.onTextChanged(GSTActivity.java:111) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextView.setText(TextView.java:2691) 
10-08 10:46:19.149: ERROR/AndroidRuntime(695):  at android.widget.TextVie 

我不知道我錯了。 。
請幫我實施這樣的方法。 謝謝。

+0

什麼是GSTActivity。第66行 – viv

+0

GSTActivity是我的應用程序的活動。 –

回答

0

儘量減少你的代碼中的多個類型的執行這樣說

你正在做

if(!(includedText.getText().toString().equals(""))) 
       { 
        double included = Double.parseDouble(includedText.getText().toString()); 

... 

可以通過這也是

String stringIncluded=includedText.getText().toString(); 
if(!(stringIncluded.equals(""))) 
        { 
         double included = Double.parseDouble(stringIncluded); 

我的意思是說試試做刪除這些常見的代碼,以便執行過程緊湊並可能導致您的問題得到解決

+0

它會解決問題嗎? –

相關問題