0

我面临着一个奇怪的情况。这应该是一个单位转换器,但它总是将来自 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);

  }
}
4

2 回答 2

2

您的字符串比较看起来非常错误:

s1 + s2.equals("IardeIarde") != null

这附加s1到 的值s2.equals("IardeIarde"),它是一个布尔值。我不知道你为什么要与 null 进行比较,因为看到你会得到truefalse在字符串中,它总是会通过比较。如果您试图将布尔值与 null 进行比较,那是行不通的。

您可能想要存储s1 + s2在一个变量中,也许比比较时更具描述性s3和调用equals

String descriptiveName = s1 + s2;
if (descriptiveName.equals("IardeLeghe"))
    ...

如果 Java for Android 支持它(我不知道,它在 Java 7 及更高版本中),你可以通过打开字符串来使你的代码更清晰:

switch (s1 + s2) {
    case "IardeLeghe":
        ...
        break;
    case "IardeLeghe":
        ...
        break;
    ...
}
于 2013-11-12T21:09:40.763 回答
0

就像宗政礼所说的,s1 + s2.equals("IardeIarde") != null基本上是在说:

s1该字符串添加s2.equals("IardeIarde")了 boolean != null。您很可能会因此收到警告。

if()语句采用trueorfalse1or的条件0。您甚至可能一开始都不会编译。

也许你正在看这个:

String s3 = s1 + s2;
if(s3.equals("Whatever")){
    // Do something
}
于 2013-11-12T21:16:39.977 回答