gpt4 book ai didi

java - 安卓 |货币转换器实时汇率

转载 作者:行者123 更新时间:2023-12-01 14:59:00 26 4
gpt4 key购买 nike

我制作了一个简单的货币转换器,但是有没有一种简单的方法来检索实时货币(只是货币,例如从以色列谢克尔到美元)?例如:

case R.id.euro:
mDollar.setChecked(false);
Meuro.setChecked(true);
exchangeRate = **//live currency from external source**
exchangeSymbol = "€";

break;

这是我的完整代码

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mResult = (TextView) findViewById(R.id.result);
mToConvert = (EditText) findViewById(R.id.toConvert);
mRadioGroup = (RadioGroup) findViewById(R.id.radioG);
mDollar = (RadioButton) findViewById(R.id.dollar);
Meuro = (RadioButton) findViewById(R.id.euro);

mToConvert.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable arg0) {
convertCurrentAmount();

}
});
mRadioGroup
.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
convertCurrentAmount();
}
});
}

public void convertCurrentAmount() {
double exchangeRate = -1;
String exchangeSymbol = null;
switch (mRadioGroup.getCheckedRadioButtonId()) {
case R.id.dollar:
mDollar.setChecked(true);
Meuro.setChecked(false);
exchangeRate = 3.76;
exchangeSymbol = "$";


break;

case R.id.euro:
mDollar.setChecked(false);
Meuro.setChecked(true);
exchangeRate = 5;
exchangeSymbol = "€";

break;
}
if (exchangeRate > 0 && exchangeSymbol != null) {

Double stringtoint = Double
.valueOf(mToConvert.getText().toString());
double result = stringtoint * exchangeRate;
mResult.setText("" + exchangeSymbol + result);

}
}

}

最佳答案

欧洲中央银行有一个以 XML 形式提供每日货币汇率的网站。

http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

这里有更多信息:http://www.ecb.int/stats/exchange/eurofxref/html/index.en.html#dev

您可能应该在启动时加载并解析 XML,并将其保存以供离线使用,这样您就不必每次都获取值(每天一次就足够了)。

关于java - 安卓 |货币转换器实时汇率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13959026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com