我刚开始学习 Android/Java 编程。这是我的第一个基本程序,现在运行良好。感谢堆栈溢出人员帮助我。现在,如果您能帮助我调整这个程序,那将是非常有帮助的,它将让我更好地了解 Android 编程的基本结构。这是代码...(简而言之:它将在您键入时将摄氏度转换为华氏度,反之亦然..)
package mag.com.myhelloworldapp;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
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.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
((EditText)findViewById(R.id.editText2)).setText("");
((EditText)findViewById(R.id.editText1)).setText("");
}
});
farenheitText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
((EditText)findViewById(R.id.editText2)).setText("");
((EditText)findViewById(R.id.editText1)).setText("");
}
});
farenheitText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence S, int start, int before, int count)
{
if(farenheitText.hasFocus())
{
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;
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
}
});
celsiusText.addTextChangedListener(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){
if (celsiusText.hasFocus())
{
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;
}
}
}
});
}
//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>
再次感谢您的帮助..