应用程序将冻结值设置为double值,以使用打字机编辑2个文本 [英] Application get freeze whete set double value to edit text using typewriter for 2 edit text

查看:75
本文介绍了应用程序将冻结值设置为double值,以使用打字机编辑2个文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款应用,我想将货币值从一种更改为另一种,就像我有2个edittext一样.一种用于输入美元货币,另一种用于输入欧元货币. 现在,我想在1个edittext中输入值,并且计算出的值应在其他编辑文本框中以相同的方式显示.

I am developeing app where i want to change the currency value from one to another, Like i have 2 edittext. one for USD Currency to enter and other for EURO currency to enter. Now i want to enter value in 1 edittext and the calculated value should display in other and same for the other edit text box.

 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("");
    }

    }
}

推荐答案

这是因为您遇到了无限循环.

It is because you get infinite loop.

amount2ET.setText将切换另一个"afterTextChanged",让您重复并重复调用Start_Calculate.

amount2ET.setText will toggle another "afterTextChanged" let you repeat and repeat call of Start_Calculate.

在设置文本之前,您应该删除监听器

You should remove the listener before set the text

 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:

Or you can set a global flag true when updating so you can dismiss another 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:

Inside Start_Calculate:

updating = true;

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


updating = false;

这篇关于应用程序将冻结值设置为double值,以使用打字机编辑2个文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆