我面临着一个奇怪的情况。这应该是一个单位转换器,但它总是将来自 edittext 的输入放入 textview 而不执行数学运算。测试是从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);
}
}