2017-09-15 85 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);