我在我的 Android 应用程序中遇到了一个问题,我的 switch/case 语句在我的处理程序中不起作用。我做 switch/case 语句的主要原因是限制我拥有的处理程序的数量。这是我的代码的一部分:
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR")
&& edittextdollars.length() > 0
&& edittexteuros.length() == 0) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
handler.sendEmptyMessage(2);
convertvalues("USD", "EUR");
img1.setImageDrawable(grabImageFromUrl(imageUrl1));
} catch (Exception e) {
edittexteuros.setText("Error");
}
}
});
thread.start();
}
if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR")
&& edittexteuros.length() > 0
&& edittextdollars.length() == 0) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
handler.sendEmptyMessage(3);
convertvalues2("EUR", "USD");
img1.setImageDrawable(grabImageFromUrl(imageUrl2));
} catch (Exception e) {
}
}
});
thread.start();
}
if (text1.equals("Euro - EUR") && text2.equals("US Dollar - USD")
&& edittextdollars.length() > 0
&& edittexteuros.length() == 0) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
handler.sendEmptyMessage(4);
convertvalues("EUR", "USD");
img1.setImageDrawable(grabImageFromUrl(imageUrl2));
} catch (Exception e) {
}
}
});
thread.start();
}
if (text1.equals("Euro - EUR") && text2.equals("US Dollar - USD")
&& edittexteuros.length() > 0
&& edittextdollars.length() == 0) {
convertvalues2("USD", "EUR");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
handler.sendEmptyMessage(5);
convertvalues2("USD", "EUR");
img1.setImageDrawable(grabImageFromUrl(imageUrl2));
} catch (Exception e) {
}
}
});
thread.start();
}
if (text1.equals("Euro - EUR") && text2.equals("Euro - EUR")
&& edittextdollars.length() > 0
&& edittexteuros.length() == 0) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
handler.sendEmptyMessage(6);
convertEurostoEuros();
//img1.setImageDrawable(grabImageFromUrl(imageUrl1));
} catch (Exception e) {
}
}
});
thread.start();
}
if (text1.equals("Euro - EUR") && text2.equals("Euro - EUR")
&& edittexteuros.length() > 0
&& edittextdollars.length() == 0) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
handler.sendEmptyMessage(7);
OppositeOfConvertEurostoEuros();
} catch (Exception e) {
}
}
public Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
dialog1.dismiss();
try {
switch (msg.what) {
case 2:
img1.setImageDrawable(grabImageFromUrl(imageUrl1));
convertvalues("USD", "EUR");
break;
case 3:
convertvalues2("EUR", "USD");
img1.setImageDrawable(grabImageFromUrl(imageUrl2));
break;
case 4:
convertvalues("EUR", "USD");
img1.setImageDrawable(grabImageFromUrl(imageUrl2));
break;
case 5:
convertvalues2("USD", "EUR");
img1.setImageDrawable(grabImageFromUrl(imageUrl2));
break;
case 6:
convertEurostoEuros();
break;
case 7:
OppositeOfConvertEurostoEuros();
break;
case 8:
convertDollarstoDollars();
break;
} catch (Exception e) {
e.printStackTrace();
}
}
};
public String convertvalues(String convertfrom, String convertto) {
double current;
double val = Double.parseDouble(edittextdollars.getText()
.toString());
DecimalFormat df = new DecimalFormat(".##");
YahooCurrencyConverter ycc = new YahooCurrencyConverter();
try {
current = ycc.convert(convertfrom, convertto);
edittexteuros.setText(df.format(val * current));
return "passed";
} catch (Exception e) {
return "passed";
}
}
public String convertvalues2(String convertfrom2, String convertto2) {
double current;
double val = Double.parseDouble(edittexteuros.getText().toString());
DecimalFormat df = new DecimalFormat(".##");
YahooCurrencyConverter ycc = new YahooCurrencyConverter();
try {
current = ycc.convert(convertfrom2, convertto2);
edittextdollars.setText(df.format(val * current));
return "passed";
} catch (Exception e) {
return "passed";
}
}
protected void convertEurostoEuros() {
double val = Double.parseDouble(edittextdollars.getText()
.toString());
DecimalFormat df = new DecimalFormat(".##");
edittexteuros.setText(df.format(val*1));
}
protected void convertDollarstoDollars() {
double val = Double.parseDouble(edittextdollars.getText()
.toString());
DecimalFormat df = new DecimalFormat(".##");
edittexteuros.setText(df.format(val*1));
}
protected void OppositeOfConvertEurostoEuros() {
double val = Double.parseDouble(edittexteuros.getText().toString());
DecimalFormat df = new DecimalFormat(".##");
edittextdollars.setText(df.format(val * 1));
}
protected void OppositeOfConvertDollarstoDollars() {
double val = Double.parseDouble(edittexteuros.getText().toString());
DecimalFormat df = new DecimalFormat(".##");
edittextdollars.setText(df.format(val * 1));
}
我的应用程序所做的事情是,即使我想找到 INR 到 JPY 的汇率,它也会找到 USD 到 EUR 的汇率。为什么这样做?是因为这是我放入处理程序的第一个案例吗?非常感谢有关此问题的任何帮助。