Android EditText onfocuschange无效

时间:2012-07-31 04:17:10

标签: android android-edittext onfocus

我是Android编程的新手。 我的以下程序执行简单的Farenheit到Celsius转换。如果您在Farenheit EditText中输入值,则会在输入的每次击键时将其转换为摄氏度。反之亦然。

我收到以下错误:

1)我在摄氏度编辑文本中所做的更改不会反映在华氏度中。

(请不要认为我在没有调试的情况下发布在论坛中,我已经尝试了3个多小时才发布这里,以便我可以保持这个论坛的清洁)

07-29 01:59:21.189:E / AndroidRuntime(1390):java.lang.NumberFormatException:无效的浮点数:“”

以下是我的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) {
               }
            public void onTextChanged(CharSequence S,int start,int before, int count){
                float inputValue;
                if (!S.toString().equals(""))
                {
                    inputValue = Float.parseFloat(S.toString());
                             ((EditText)findViewById(R.id.editText1)).setText(String
                                    .valueOf(convertFahrenheitToCelsius(inputValue)));

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



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

                   }

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

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

                if((v == findViewById(R.id.editText2)) &&  (hasFocus==true)) 
                {
                farenheitText.addTextChangedListener(watcher1);
                }
                else if ((v == findViewById(R.id.editText1)) &&  (hasFocus==true)) 
                {
                    ((EditText)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>

我现在只学习Java编程(我是一名SQL程序员)。

2 个答案:

答案 0 :(得分:1)

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

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

我认为当您在(OnFocusChangeListener)

(OnFocusChangeListener)行中将this输入OnFocusChangeListener时,您会收到异常

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

    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)

答案 1 :(得分:1)

有些时候clearfocus()解决了这个问题。尝试在editText1.clearfocus()方法或其他合适的代码位置中调用onCreate()