前端代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/et_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:hint="请输入年份" /> <EditText android:id="@+id/et_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:hint="请输入月份" /> <Button android:id="@+id/but_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:onClick="onClickButton" android:text="查询" /> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="查询结果" android:textSize="30sp" /> </LinearLayout>
后端代码
package com.example.comzhaoshijidays; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private EditText Et_month; private EditText Et_year; private TextView Vt_show; int int_yer; int int_mon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Et_month = findViewById(R.id.et_2); Et_year = findViewById(R.id.et_1); Vt_show = findViewById(R.id.tv_1); } public static int Year(int i) { int c; if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) { c = 1; return c = 1; } else { return c = 0; } } public void onClickButton(View view) { String str_year = Et_year.getText().toString(); int_yer = Year(Integer.parseInt(str_year)); int_mon = Month(Integer.parseInt(Et_month.getText().toString())); if (int_yer == 1) { Vt_show.setText(Et_year.getText().toString() + "年" + "是润年" + "," + Et_month.getText().toString() + "月共有" + int_mon + "天"); } else { Vt_show.setText(Et_year.getText().toString() + "年" + "是平年" + "," + Et_month.getText().toString() + "月共有" + int_mon + "天"); } } private int Month(int i) { int day = 0; switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 4: case 6: case 9: case 11: day = 30; break; default: if (int_yer == 1) { day = 29; } else { day = 28; } } return day; } }