问题描述
我是新来的Android编程。
我下面的程序做一个简单的华氏摄氏转换。如果您在华氏度的EditText输入值,将其转换成摄氏进入每个按键。反之亦然。
I'm new to Android programming. My following program does a simple Farenheit to Celsius conversion. If you enter values in a Farenheit EditText it will convert it into Celsius for every keystroke entered. And Vice versa.
我收到以下错误:
1)我在做摄氏编辑文本的变化不会反映在华氏温度。
1) The Changes I make in Celsius edit text are not reflected in Fahrenheit.
(请不要认为我在论坛上贴出无需调试,我已经尝试了3个多小时在这里发帖之前,这样我可以保持这个论坛干净)
(please don't think I posted in forum without debugging, I have tried for more than 3 hours before posting here so that I can keep this forum clean )
1月7日至29日:59:21.189:E / AndroidRuntime(1390):java.lang.NumberFormatException:无效的浮动:
07-29 01:59:21.189: E/AndroidRuntime(1390): java.lang.NumberFormatException: Invalid float: ""
以下是我MainActivity.Java
Following is my 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中是
My Activity_main.xml is
<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程序员)。
I'm learning Java programming now only(I'm a SQL programmer).
推荐答案
您必须在活动类实现OnFocusChangeListener。
you must implement OnFocusChangeListener in your activity class.
public class MainActivity extends Activity implements OnFocusChangeListener
{
....
@Overrride
public void onFocusChange(View v, boolean hasFocus)
{
....
}
....
}
我想你会得到一个异常时类型转换这个
到 OnFocusChangeListener
在该行的 (OnFocusChangeListener)本
的 修改::: 的
我进去一看乌尔code。我觉得你的实现应该是这样的:
::I had a look into ur code. I think your implementation should be like this:
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这个code和删除线 celsiusText.setOnFocusChangeListener((OnFocusChangeListener)本)
和 ferenheitText.setOnFocusChangeListener((OnFocusChangeListener )本)
这篇关于Android版的EditText onfocuschange不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!