2017-10-22 9 views
0

나는 2 개의 edittext와 같이 하나의 통화 값을 다른 것으로 변경하려는 앱을 개발 중입니다. 하나는 USD 통화를 입력하고 다른 하나는 EURO 통화를 입력하는 것입니다. 이제 1 edittext에 값을 입력하고 계산 된 값이 다른 편집 텍스트 상자에 대해 동일하게 표시되어야합니다.응용 프로그램이 고정시키다 whete는 2 개의 편집 텍스트에 대해 타자기를 사용하여 텍스트를 편집하기 위해 double 값을 설정합니다.

TextWatcher inputTextWatcher = new TextWatcher() 
    { 
     public void afterTextChanged(Editable s) 
     { 

      try { 
       Start_Calculate(""+s.toString()); 
       } 
       catch (NumberFormatException nfe) 
       { //or whatever exception you get 
       } 
         //do some handling if you need to 



     } 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) 
     { 

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

     } 
    }; 

    amountET.addTextChangedListener(inputTextWatcher); 


    TextWatcher inputTextWatcher1 = new TextWatcher() { 
     public void afterTextChanged(Editable s) 
     { 


      try { 
       Start_Calculate2(""+s.toString()); 
       } 
       catch (NumberFormatException nfe) 
       { //or whatever exception you get 
       } 

     } 
     public void beforeTextChanged(CharSequence s, int start, int count, int after){ 
     } 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
     } 
    }; 

    amount2ET.addTextChangedListener(inputTextWatcher1); 

기능 현재 위치 : 당신이 무한 루프를 얻을 수 있기 때문에

public void Start_Calculate(String val) 
{ 

     String user_input_value="00.00"; 

    try 
    { 
    // user_input_value=""+amountET.getText().toString(); 
     user_input_value=""+val; 
    } 
    catch(NumberFormatException e) 
    { 
     user_input_value="00.00"; 
    } 
    //*/ 

    if(!user_input_value.equalsIgnoreCase("")) 
    { 

    if(user_input_value.length()<11) 
    { 
     try 
     { 
      user_amount=Double.parseDouble(""+user_input_value); 
     } 
     catch(NumberFormatException e) 
     { 
      user_amount=00.000; 
     } 



     if(user_amount>0) 
     { 

      user_amount=user_amount*1.0000001; 
      total_amount_to_send=((user_amount*to_amount_val)/from_amount_val); 
     // total_amount_to_send= total_amount_to_send+00.0000; 
      //String total=new DecimalFormat("##.##").format(total_amount_to_send); 
     // totalTV.setText(user_amount+" "+fromTV.getText().toString()+" = "+ total+" ("+toTV.getText().toString()+")"); 
      // totalTV.setText(user_amount+" = "+ total); 


      // String finalVal= df.format(target_currency_val); 

      //total_calTV.setText("("+user_amount+" × "+df.format(to_amount_val) +" = "+ total+")"); 

      String total="00.00"; 
      DecimalFormat df = new DecimalFormat("#.####"); 
      total=""+df.format(total_amount_to_send); 

      try 
      { 
      amount2ET.setText(""+total);//_amount_to_send+""); 
      } 
      catch(Exception e) 
      { 
       Log.e("Error in Calculate1: ",""+e.getMessage()); 
       } 

      //showtoast(""+total);//_amount_to_send); 

      //usdTV.setText(""+user_amount+" ("+fromTV.getText().toString()+")"); 
      //pkrTV.setText(""+ total+" ("+toTV.getText().toString()+")"); 


     } 
     else 
     { 


     } 
    } 
    else 
    { 

    } 

    } 
} 


public void Start_Calculate2(String val) 
{ 

     //String user_input_value="0"; 
    //String user_input_value=""+val; 
    String user_input_value="0"; 

    try 
    { 
    //user_input_value=""+amount2ET.getText().toString(); 
     user_input_value=val; 
    } 
    catch(NumberFormatException e) 
    { 
     user_input_value="00.00"; 
    } 
    //*/ 
    if(!user_input_value.equalsIgnoreCase("")) 
    { 

    if(user_input_value.length()<11) 
    { 
     try 
     { 
      user_amount=Double.parseDouble(""+user_input_value); 
     } 
     catch(NumberFormatException e) 
     { 
      user_amount=00.00; 
     } 

     if(user_amount>0) 
     { 

      user_amount=user_amount*1.0000001; 

      total_amount_to_send=((user_amount*from_amount_val)/to_amount_val); 
     // total_amount_to_send= total_amount_to_send+00.0000; 
      //String total=new DecimalFormat("##.##").format(total_amount_to_send); 
      // totalTV.setText(user_amount+" "+toTV.getText().toString()+" = "+ total+" ("+fromTV.getText().toString()+")"); 

      // totalTV.setText(user_amount+" = "+ total); 

       //DecimalFormat df = new DecimalFormat("#.##"); 
      // String finalVal= df.format(target_currency_val); 

      //total_calTV.setText("("+user_amount+" × "+df.format(to_amount_val) +" = "+ total+")"); 





      String total=new DecimalFormat("##.##").format(total_amount_to_send); 

      try 
      { 
      amountET.setText(""+total);//_amount_to_send); 
      } 
      catch(Exception e) 
      { 
       Log.e("Error in Calculate-2: ",""+e.getMessage()); 
       } 

      //showtoast(""+total);//_amount_to_send); 
      //usdTV.setText(""+user_amount+" ("+fromTV.getText().toString()+")"); 
      //pkrTV.setText(""+ total+" ("+toTV.getText().toString()+")"); 


     } 
     else 
     { 
      //totalTV.setText(""); 
      //total_calTV.setText(""); 
     } 
    } 
    else 
    { 
     //totalTV.setText(""); 
     //total_calTV.setText(""); 
    } 

    } 
} 

답변

1

그것은이다.

amount2ET.setText는 "afterTextChanged"를 토글하여 Start_Calculate를 반복하고 반복하도록합니다.

당신은 청취자 전에 텍스트

amountET.removeTextChangedListener(inputTextWatcher) 

      try 
      { 
      amountET.setText(""+total);//_amount_to_send); 
      } 
      catch(Exception e) 
      { 
       Log.e("Error in Calculate-2: ",""+e.getMessage()); 
       } 

amountET.addTextChangedListener(inputTextWatcher); 

을 설정 제거해야합니다 또는 그래서 당신은 다른 Start_Calculate 해제 할 수 업데이트 할 때 당신은 진정한 글로벌 플래그를 설정할 수 있습니다

boolean updating = false; 

public void afterTextChanged(Editable s) 
     { 

      try { 
       if (!updating) 
       Start_Calculate(""+s.toString()); 
       } 
       catch (NumberFormatException nfe) 
       { //or whatever exception you get 
       } 
         //do some handling if you need to 



     } 

내부 Start_Calculate을 :

updating = true; 

     try 
     { 
     amountET.setText(""+total);//_amount_to_send); 
     } 
     catch(Exception e) 
     { 
      Log.e("Error in Calculate-2: ",""+e.getMessage()); 
      } 


updating = false;