0

我正在编写和 android 应用程序,我的代码是货币转换器,当我输入要转换的金额时,什么都不会改变 - 转换后的金额为零 -

我不知道我的代码有什么问题:

这是我正在编写的代码:

public class Currency_convert extends Activity
{
    /** Called when the activity is first created. */

    float inputValue;
    Handler h;
    static int decimalsize = 2;
    public DecimalFormat df = new DecimalFormat();

    String US = "USD";
    String EURO = "EUR";
    String INR = "INR";
    String AUD = "AUD";
    String GBP = "GBP";

    public double dUS;// = 0.0;
    public double dEURO = 1.0;
    public double dINR;// = 0.0;
    public double dAUD;// = 0.0;
    public double dGBP;// = 0.0;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.currency);
        boolean rateupdate = false;

        AbsoluteLayout al = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout01);
        al.setBackgroundColor(Color.LTGRAY);

        final EditText amount = (EditText) findViewById(R.id.txt1);
        final EditText etResult = (EditText) findViewById(R.id.txt2);
        final Spinner sp1 = (Spinner) findViewById(R.id.spinerr1);
        final Spinner sp2 = (Spinner) findViewById(R.id.spinerr2);

        final InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_spinner_item);
        final Button btn = (Button) findViewById(R.id.bb1);

        amount.setText("0.0");
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     //   adapter.setDropDownViewResource(android.R.layout.)
        sp1.setAdapter(adapter);
        sp2.setAdapter(adapter);

        rateupdate = getRate();
        if(rateupdate)
            btn.setText("Currency Rate updated");
        else
            btn.setText("Updated Currency Rate ");

        //btn.setText(Boolean.toString(rateupdate));

        OnClickListener ltouch = new OnClickListener() 
        {
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                //getRate();
            }
        };;;
        btn.setOnClickListener(ltouch );

        OnItemClickListener itemClick = new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
            {

                int curr1 = (int) sp1.getSelectedItemId();
                int curr2 = (int) sp2.getSelectedItemId();
                double result = Convert(curr1, curr2, amount.getText().toString());

                etResult.setText(Double.toString(result));

            }
        };

        OnItemSelectedListener ItemSelectListener = new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int position, long id) 
            {
                try
                {

                    int curr1 = (int) sp1.getSelectedItemId();
                    int curr2 = (int) sp2.getSelectedItemId();
                    double result = Convert(curr1, curr2, amount.getText().toString());
                    etResult.setText(Double.toString(result));

                }
                catch(Exception e)
                {

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub

            }
        };
        sp1.setOnItemSelectedListener(ItemSelectListener);
        sp2.setOnItemSelectedListener(ItemSelectListener);

        amount.addTextChangedListener(new TextWatcher() 
        {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) 
            {
                // TODO Auto-generated method stub
                int length = s.length();
                if(length == 0)
                    //amount.setText("0.0");
                    s = "0.0";
                int curr1 = (int) sp1.getSelectedItemId();
                int curr2 = (int) sp2.getSelectedItemId();
                //double result = Convert(curr1, curr2, amount.getText().toString());
                double result = Convert(curr1, curr2, s.toString());

                etResult.setText(Double.toString(result));      
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) 
            {
                // TODO Auto-generated method stub
            }
            @Override
            public void afterTextChanged(Editable s) 
            {
                // TODO Auto-generated method stub
            }
        });
        OnTouchListener l = new OnTouchListener() 
        {
            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                // TODO Auto-generated method stub
                imm.hideSoftInputFromWindow(amount.getWindowToken(),0);//
                return true;
            }
        }; 
        al.setOnTouchListener(l);
        sp1.setSelection(4);
        sp2.setSelection(0);
    }

    protected Double Convert(int currency_from_index, int currency_to_index, String s_amount) 
    {
        // TODO Auto-generated method stub
        Double result = 0.0;
        Double amount = Double.parseDouble(s_amount);
        Double source = 0.0;
        Double destination = 0.0;

        if((dUS * dAUD * dINR * dGBP * dEURO) == 0)
        {
            result = 0.0;
            return result;
        }
        else
        {

            switch (currency_from_index) 
            {
            case 0:
                source = dUS;
                break;
            case 1:
                source = dAUD;
                break;
            case 2:
                source = dINR;
                break;
            case 3:
                source = dGBP;
                break;
            case 4:
                source = dEURO;
                break;
            default:
                break;
            }

            switch (currency_to_index)
            {
            case 0:
                destination = dUS;
                break;
            case 1:
                destination = dAUD;
                break;
            case 2:
                destination = dINR;
                break;
            case 3:
                destination = dGBP;
                break;
            case 4:
                destination = dEURO;
                break;
            default:
                break;
            }

            result = amount*destination/source;
            Math.round(result);
            try
            {
            df.setMaximumFractionDigits(decimalsize);   
            df.setMinimumFractionDigits(decimalsize);
            String str_result = df.format(result);
            result = Double.parseDouble(str_result);
            }
            catch(Exception e)          
            {

            }

            return result;
        }

    }


    public boolean getRate()
    {
        //ConversionRate cr = new ConversionRate();
        try
        {

         //URL url = new URL(eText.getText().toString());
            String path = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
        URL url = new URL(path);
        URLConnection conn = url.openConnection();
        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        //xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
        xpp.setInput(rd);
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) 
        {
         if(eventType == XmlPullParser.START_DOCUMENT) 
         {
             //System.out.println("Start document");
             //tView.append("Start document");
         } 
         else if(eventType == XmlPullParser.END_DOCUMENT) 
         {
             //System.out.println("End document");
             //tView.append("End document");
         } 
         else if(eventType == XmlPullParser.START_TAG) 
         {
             //System.out.println("Start tag "+xpp.getName());
             //tView.append("Start tag "+xpp.getName());
             //tView.append("Attribute Count = " + xpp.getAttributeCount());
             if(xpp.getAttributeCount() > 1)
             {
                 /*tView.append("Currency = " + xpp.getAttributeValue(0) + "\t");
                 tView.append("Rate = " + xpp.getAttributeValue(1));
                 tView.append("\n");*/
                 //cr.setName(xpp.getAttributeValue(0));
                 //cr.setRate(Double.parseDouble(xpp.getAttributeValue(1)));
                 if(xpp.getAttributeValue(0).equals(US))// toString()== US)
                     Currency_convert.this.dUS = Double.parseDouble(xpp.getAttributeValue(1));
                 else if(xpp.getAttributeValue(0).equals(INR))// .toString() == "INR")
                     dINR = Double.parseDouble(xpp.getAttributeValue(1));
                 else if(xpp.getAttributeValue(0).equals(GBP))// .toString() == "GBP")
                     dGBP = Double.parseDouble(xpp.getAttributeValue(1));
                 else if(xpp.getAttributeValue(0).equals(AUD))// .toString() == "AUD")
                     dAUD = Double.parseDouble(xpp.getAttributeValue(1));
             } 
         } 
         else if(eventType == XmlPullParser.END_TAG) 
         {
             //System.out.println("End tag "+xpp.getName());
             //tView.append("End tag "+xpp.getName());
         } 
         else if(eventType == XmlPullParser.TEXT) 
         {
             //System.out.println("Text "+xpp.getText());
             //tView.append("Text "+xpp.getText());
         }
         eventType = xpp.next();
        }
        return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return false;
        }

    }

}

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>




<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:id="@+id/txt1" android:text="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_y="10dip" android:numeric="decimal" android:singleLine="true" android:ems="8" android:hint="Enter amount" android:layout_x="5dip"></EditText>
<EditText android:id="@+id/txt2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@+id/EditText02" android:editable="false" android:enabled="true" android:singleLine="true" android:layout_x="5dip" android:ems="8" android:layout_y="69dip"></EditText>

<Spinner android:id="@+id/spinerr1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_y="10dip" android:layout_x="180dip"></Spinner>
<Spinner android:layout_y="69dip" android:id="@+id/spinerr2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_x="180dip"></Spinner>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/bb1" android:layout_x="50dip" android:text="Update Currency Rates" android:layout_y="120dip"></Button>
</AbsoluteLayout>

我在 LogCat 中没有收到任何错误!!!

4

1 回答 1

0

代码有很多错误:

1.不要使用已弃用的绝对布局。

2.删除此代码中的注释:

OnClickListener ltouch = new OnClickListener() 
    {
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            //getRate();
        }
    };;;

3.使用AsncTask或Thread进行后台操作。

于 2013-11-07T10:16:29.330 回答