Android 系统必看的小程序,含代码推荐

Android系统是目前世界上最受欢迎的移动操作系统之一,它的应用程序数量众多,包括了各种小应用,这些小应用不仅能够提高我们的生活质量,还能够帮助我们更好地理解Android系统的工作原理。在本文中,我将介绍几个必看的小程序,并附上其代码。

1.计算器

计算器是最基本的小程序之一,它可以帮助我们进行简单的数学运算。以下是一个简单的计算器程序:

```

public class Calculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("请输入第一个数字:");

double num1 = scanner.nextDouble();

System.out.print("请输入第二个数字:");

double num2 = scanner.nextDouble();

System.out.println("请输入需要进行的运算:+、-、*、/ ");

char operator = scanner.next().charAt(0);

double result = 0.0;

switch (operator) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

result = num1 / num2;

break;

default:

System.out.println("输入的运算符有误!");

return;

}

System.out.println(num1 + " " + operator + " " + num2 + " = " + result);

}

}

```

2.天气预报

天气预报是一个非常实用的小程序,它可以帮助我们了解当地的天气情况。以下是一个基于Android Studio的天气预报程序:

```

public class WeatherActivity extends AppCompatActivity {

private TextView tvCity;

private TextView tvTemperature;

private TextView tvWeather;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_weather);

tvCity = findViewById(R.id.tv_city);

tvTemperature = findViewById(R.id.tv_temperature);

tvWeather = findViewById(R.id.tv_weather);

String city = "北京";

String url = "http://wthrcdn.etouch.cn/weather_mini?city=" + city;

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()

.url(url)

.build();

client.newCall(request).enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {

}

@Override

public void onResponse(Call call, Response response) throws IOException {

String responseData = response.body().string();

parseJsonWithGson(responseData);

}

});

}

private void parseJsonWithGson(String jsonData) {

Gson gson = new Gson();

Weather weather = gson.fromJson(jsonData, Weather.class);

runOnUiThread(new Runnable() {

@Override

public void run() {

tvCity.setText(weather.getData().getCity());

tvTemperature.setText(weather.getData().getWendu());

tvWeather.setText(weather.getData().getForecast().get(0).getType());

}

});

}

}

```

3.翻译工具

翻译工具是一个非常实用的小程序,它可以帮助我们进行多种语言之间的翻译。以下是一个基于有道翻译API的翻译工具程序:

```

public class Translator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("请输入需要翻译的内容:");

String text = scanner.nextLine();

System.out.println("请输入需要翻译的语言:1.英文 2.中文");

int type = scanner.nextInt();

String from = "";

String to = "";

if (type == 1) {

from = "EN";

to = "zh-CHS";

} else if (type == 2) {

from = "zh-CHS";

to = "EN";

}

String url = "http://fanyi.youdao.com/translate?&doctype=json&type=" + from + "2" + to + "&i=" + text;

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()

.url(url)

.build();

client.newCall(request).enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {

}

@Override

public void onResponse(Call call, Response response) throws IOException {

String responseData = response.body().string();

parseJsonWithGson(responseData);

}

});

}

private static void parseJsonWithGson(String jsonData) {

Gson gson = new Gson();

Translation translation = gson.fromJson(jsonData, Translation.class);

System.out.println(translation.getTranslateResult().get(0).get(0).getTgt());

}

}

```

以上就是几个必看的小程序,它们不仅能够帮助我们更好地理解Android系统的工作原理,同时也能够提高我们的生活质量。