我遇到了一个奇怪的情况。这应该是一个单位转换器,但它总是在texttext中输入来自edittext的输入,而不执行数学运算。 测试是从edittext中的数字进行转换,并在textview的文本中报告结果。现在它在textview中显示我在edittext中写的内容。谢谢你的时间。
entepublic class lunghezze extends Activity implements OnClickListener {
EditText et1;
TextView tv1;
Spinner spinner1,spinner2;
Button btnAdd;
String oper = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,R.array.lunghezza, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,R.array.lunghezza, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
// find the elements
et1 = (EditText) findViewById(R.id.editText1);
btnAdd = (Button) findViewById(R.id.button1);
tv1 = (TextView) findViewById(R.id.textView4);
// set a listener
btnAdd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double num1 = 0;
double resdelta = 0;
double res = 0;
double resx2 = 0;
if (TextUtils.isEmpty(et1.getText().toString())) {
return;
}
String a = et1.getText().toString().trim();
num1 = a.isEmpty()? Double.NaN:Double.parseDouble(a);
switch (v.getId()) {
case R.id.button1:
String s1 = spinner1.getSelectedItem().toString();
String s2 = spinner2.getSelectedItem().toString();
if (s1+s2.equals( "IardeIarde") != null){
res=num1;
} else if (s1+s2.equals( "IardeLeghe") != null){
res= 0.0005*num1;
} else if (s1+s2.equals( "IardeMetri") != null){
res= 0.91*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "IardeMiglia terrestri") != null){
res= 0.0005*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "IardePollici") != null){
res= 36*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "LegheIarde") != null){
res= 5280*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "LegheLeghe") != null){
res=num1;
} else if (s1+s2.equals( "LegheMetri") != null){
res= 4828*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "LegheMiglia terrestri") != null){
res= 3*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "LeghePollici") != null){
res= 190080*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "MetriIarde") != null){
res= 1.02*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "MetriLeghe") != null){
res= 0.0002*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "MetriMetri") != null){
res=num1;
} else if (s1+s2.equals( "MetriMiglia terrestri") != null){
res= 0.0006*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "MetriPollici") != null){
res= 39.3*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "Miglia terrestriIarde") != null){
res= 1760*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "Miglia terrestriLeghe") != null){
res= 0.33*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "Miglia terrestriMetri") != null){
res= 1609.3*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "Miglia terrestriMiglia terrestri") != null){
res=num1;
} else if (s1+s2.equals( "Miglia terrestriPollici") != null){
res= 63360*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "PolliciIarde") != null){
res= 0.027*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "PolliciLeghe") != null){
res= 0.0000052*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "PolliciMetri") != null){
res= 0.0254*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "PolliciMiglia terrestri") != null){
res= 0.000015*num1;
tv1.setText("" + res);
} else if (s1+s2.equals( "PolliciPollici") != null){
res=num1;
} else {
}
break;
default:
break;
}
tv1.setText("" + res);
}
}
答案 0 :(得分:2)
你的字符串比较看起来非常错误:
s1 + s2.equals("IardeIarde") != null
这会将s1
附加到s2.equals("IardeIarde")
的值,这是一个布尔值。我不知道为什么你要比较null,因为看到你在字符串中得到true
或false
,它总是会通过比较。如果你试图将布尔值与null进行比较,那么,这是行不通的。
您可能希望将s1 + s2
存储在变量中,可能比s3
更具描述性,并在比较时仅调用equals
。
String descriptiveName = s1 + s2;
if (descriptiveName.equals("IardeLeghe"))
...
如果Android for Java支持它(我不知道,它在Java 7及更高版本中),您可以通过打开字符串来使代码更清晰:
switch (s1 + s2) {
case "IardeLeghe":
...
break;
case "IardeLeghe":
...
break;
...
}
答案 1 :(得分:0)
s1 + s2.equals("IardeIarde") != null
基本上是在说:
s1
字符串添加s2.equals("IardeIarde")
布尔值!= null
。你很可能会因此而得到警告。
if()
语句的条件为true
或false
,或1
或0
。你可能甚至都不会在开始时编译。
也许你正在看这个:
String s3 = s1 + s2;
if(s3.equals("Whatever")){
// Do something
}