2012-07-31 90 views
1

我是Android编程新手。 我的下面的程序做了一个简单的华氏转换为摄氏温度。如果您在Farenheit EditText中输入数值,则每输入一次击键,它都会将其转换为摄氏温度。反之亦然。Android EditText onfocuschange不起作用

,我发现了以下错误:

1)我做在摄氏编辑文本的变化不会反映在华氏温度。

(请不要认为我在论坛上张贴无需调试,我已经尝试了3个多小时在这里发帖之前,这样我可以保持这个论坛干净)

1月7日至29日: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程序员)。

+0

问题是在这里java.lang.NumberFormatException:无效的浮点数:“”。或许你正试图将一个空字符串转换为浮点数。 – 2012-07-31 04:35:54

回答

1

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

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

我想你会得到你时,类型转换thisOnFocusChangeListener在行(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)

+0

当我声明onFocusChange内部活动时,它在onTextChanged方法中抛出错误(同一方法的重复定义...)。正如你所说的,它首先在行(OnFocusListener)中引发了错误,但是当我清理该项目时,错误消失了。 – Mahesh 2012-07-31 04:28:24

+0

我已经更新了我的答案,请看看它。 – 2012-07-31 05:09:00

+0

Sure PC ..我会尽快更新你,只要我在大概5个小时左右就可以得到我的答案..非常感谢... – Mahesh 2012-07-31 08:38:29

1

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

+0

谢谢你解决了我的问题:-) – 2014-10-16 19:52:05

相关问题