应用程序获取冻结白色设置双值以使用打字机编辑文本以进行 2 编辑文本

Posted

技术标签:

【中文标题】应用程序获取冻结白色设置双值以使用打字机编辑文本以进行 2 编辑文本【英文标题】:Application get freeze whete set double value to edit text using typewriter for 2 edit text 【发布时间】:2017-10-22 11:43:36 【问题描述】:

我正在开发应用程序,我想将货币价值从一种更改为另一种,就像我有 2 个编辑文本一样。一个用于输入美元货币,另一个用于输入欧元货币。 现在我想在 1 个编辑文本中输入值,计算出的值应该显示在其他编辑文本框中。

 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);

或者您可以在更新时将全局标志设置为 true,这样您就可以关闭另一个 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;

【讨论】:

以上是关于应用程序获取冻结白色设置双值以使用打字机编辑文本以进行 2 编辑文本的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Xcode 中获取 JSONarray 值以显示在文本字段中? [关闭]

从文本字段复制值以选择下拉列表[重复]

使用 select from 从表中获取值以存储在插入表中

OS X App使用逗号转换文本字段导致双值问题

在编辑另一个文本字段 vue.js 时获取计算属性并传递其值

按键后如何获取编辑文本的文本