Android设计一个数值转化的页面的代码
时间: 2024-03-06 10:48:02 浏览: 89
以下是一个简单的 Android 数值转化页面的代码,包括两个 EditText 输入框和两个 Button 按钮,可以将输入的数字进行摄氏度和华氏度之间的转换:
1. 布局文件 activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextCelsius"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/celsius"/>
<EditText
android:id="@+id/editTextFahrenheit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/fahrenheit"/>
<Button
android:id="@+id/buttonCelsiusToFahrenheit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/celsius_to_fahrenheit"/>
<Button
android:id="@+id/buttonFahrenheitToCelsius"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fahrenheit_to_celsius"/>
</LinearLayout>
```
2. 字符串资源文件 strings.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Number Converter</string>
<string name="celsius">Celsius</string>
<string name="fahrenheit">Fahrenheit</string>
<string name="celsius_to_fahrenheit">Convert Celsius to Fahrenheit</string>
<string name="fahrenheit_to_celsius">Convert Fahrenheit to Celsius</string>
</resources>
```
3. Java 代码 MainActivity.java:
```java
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextCelsius, editTextFahrenheit;
private Button buttonCelsiusToFahrenheit, buttonFahrenheitToCelsius;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextCelsius = findViewById(R.id.editTextCelsius);
editTextFahrenheit = findViewById(R.id.editTextFahrenheit);
buttonCelsiusToFahrenheit = findViewById(R.id.buttonCelsiusToFahrenheit);
buttonFahrenheitToCelsius = findViewById(R.id.buttonFahrenheitToCelsius);
editTextCelsius.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
double celsius = Double.parseDouble(s.toString());
double fahrenheit = celsiusToFahrenheit(celsius);
editTextFahrenheit.setText(String.valueOf(fahrenheit));
} else {
editTextFahrenheit.setText("");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
editTextFahrenheit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
double fahrenheit = Double.parseDouble(s.toString());
double celsius = fahrenheitToCelsius(fahrenheit);
editTextCelsius.setText(String.valueOf(celsius));
} else {
editTextCelsius.setText("");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
buttonCelsiusToFahrenheit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editTextCelsius.getText().length() > 0) {
double celsius = Double.parseDouble(editTextCelsius.getText().toString());
double fahrenheit = celsiusToFahrenheit(celsius);
editTextFahrenheit.setText(String.valueOf(fahrenheit));
}
}
});
buttonFahrenheitToCelsius.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editTextFahrenheit.getText().length() > 0) {
double fahrenheit = Double.parseDouble(editTextFahrenheit.getText().toString());
double celsius = fahrenheitToCelsius(fahrenheit);
editTextCelsius.setText(String.valueOf(celsius));
}
}
});
}
private double celsiusToFahrenheit(double celsius) {
return celsius * 1.8 + 32;
}
private double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) / 1.8;
}
}
```
这个页面实现了一个简单的摄氏度和华氏度之间的转换功能,通过输入摄氏度或华氏度,或者点击对应的按钮,即可进行转换。需要注意的是,为了避免用户输入非法字符导致程序崩溃,需要进行异常处理。
阅读全文