2017-09-15 45 views
0

我有2個EditText接收數值,我需要總結這兩個值並在TextView中顯示dinamically。我沒有一個按鈕來開始總和,所以當用戶鍵入TextView時需要自動更改。更新TextView動態使用RxJava

我試過用TextWatcher,但是當用戶在同一個EditText中鍵入2個數字(如果他鍵入「1」而不是「2」,TextView顯示「3」而不是「12」)

這裏是我的XML:

<EditText 
    android:layout_height="wrap_content" 
    android:layout_width="200dp" 
    android:id="@+id/edit1" /> 
    <EditText 
    android:layout_height="wrap_content" 
    android:layout_width="200dp" 
    android:id="@+id/edit2" /> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/text1"/> 

我的Java代碼:

editText1.addTextChangedListener(new TextWatcher(){ 
     DecimalFormat dec = new DecimalFormat("0.00"); 
     @Override 
     public void afterTextChanged(Editable arg0) { 
      if(!arg0.toString().equals(current)){ 

       String edittext1_value = arg0.toString(); 
       int total = Integer.parseInt(edittext1_value + edittext2_value) 


      } 
     } 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, 
             int count, int after) { 
      finish_contribuir.setVisibility(View.VISIBLE); 
      add_more.setVisibility(View.VISIBLE); 
     } 
     private String current = ""; 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int 
     count) { 
      if(!s.toString().equals(current)){ 

      } 
     } 
    }); 

一些在這裏辦公的傢伙說,也許RxJava可以解決我的問題

回答

1

替換:

Integer.parseInt(edittext1_value + edittext2_value)  

有:

Integer.parseInt(edittext1_value)+ Integer.parseInt(edittext2_value);