2

我是 Android 编程新手。我下面的程序做了一个简单的华氏到摄氏转换。如果您在 Farenheit 中输入值,EditText它将在每次输入的按键时将其转换为摄氏度。

我收到以下错误,我在摄氏编辑文本中所做的更改未反映在华氏温度中。

07-29 01:59:21.189: E/AndroidRuntime(1390): java.lang.NumberFormatException: Invalid float: ""

以下是我的MainActivity.java

 public class MainActivity extends Activity {
    private EditText celsiusText;
    private EditText farenheitText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        celsiusText = (EditText) findViewById(R.id.editText1);
        farenheitText = (EditText) findViewById(R.id.editText2);
        celsiusText.requestFocus();
        celsiusText.setOnFocusChangeListener((OnFocusChangeListener) this);
        farenheitText.setOnFocusChangeListener((OnFocusChangeListener) this);
    }

    public void onFocusChange(View v, boolean hasFocus) {            
         TextWatcher watcher1 = new TextWatcher() {
             public void afterTextChanged(Editable s) {

             }

             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                 // empty
             }

             public void onTextChanged(CharSequence S,int start,int before, int count) {
                 float inputValue;
                 if (!S.toString().equals("")) {
                     inputValue = Float.parseFloat(S.toString());
                     (findViewById(R.id.editText1)).setText(String
                               .valueOf(convertFahrenheitToCelsius(inputValue)));
                  } else {
                      (findViewById(R.id.editText1)).setText("");
                      return;
                  }
              }
          };

          TextWatcher watcher2 = new TextWatcher() {
               public void afterTextChanged(Editable s) {
                   // empty
               }

               public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                   // empty
               }

               public void onTextChanged(CharSequence S,int start,int before, int count) {
                    float inputValue;
                    if (!S.toString().equals("")) {
                        inputValue = Float.parseFloat(S.toString());
                        (findViewById(R.id.editText2)).setText(String
                                .valueOf(convertCelsiusToFahrenheit(inputValue)));

                     } else {
                         (findViewById(R.id.editText2)).setText("");
                         return;
                     }
                }
            };

            if((v == findViewById(R.id.editText2)) && hasFocus) {
                farenheitText.addTextChangedListener(watcher1);
            } else if ((v == findViewById(R.id.editText1)) && hasFocus) {
                (findViewById(R.id.editText1)).setText("");
                celsiusText.addTextChangedListener(watcher2);
            }
      }

      //Converts to celsius
      private float convertFahrenheitToCelsius(float fahrenheit) {
          return ((fahrenheit - 32) * 5 / 9);
      }

      // Converts to fahrenheit
      private float convertCelsiusToFahrenheit(float celsius) {
           return ((celsius * 9) / 5) + 32;
      }

}

我的activity_main.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="128dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="62dp"
        android:ems="10"
        android:inputType="numberSigned" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="128dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="numberSigned" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="18dp"
        android:text="@string/celsius"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignRight="@+id/editText2"
        android:text="@string/fahrenheit"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
4

2 回答 2

2

有时clearfocus()可以解决这个问题。尝试调用方法或其他合适editText1.clearfocus()onCreate()代码位置。

于 2014-08-12T11:34:45.617 回答
1

您必须在活动类中实现 OnFocusChangeListener。

public class MainActivity extends Activity implements OnFocusChangeListener
{
    ....
    @Overrride
    public void onFocusChange(View v, boolean hasFocus)
    {
        ....
    }
    ....
}

我认为当您在该行 (OnFocusChangeListener) 键入 cast 时thisOnFocusChangeListener 出现异常

编辑::: 我查看了你的代码。我认为你的实现应该是这样的:

    celsiusText.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            ....
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            ....
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            ....
        }
    });
    farenheitText.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            ....
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            ....
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            ....
        }
    });

将此代码放入 oncreate 并删除行celsiusText.setOnFocusChangeListener((OnFocusChangeListener) this)ferenheitText.setOnFocusChangeListener((OnFocusChangeListener) this)

于 2012-07-31T04:19:40.353 回答