如图所示,我需要实现左右方向键增减日期,代码该如何实现?布局我已经实现,如下所示:
<?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:background="@mipmap/bg_bl1"
android:orientation="vertical">
<LinearLayout
android:id="@+id/distance_excel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="@+id/distance_excel_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/mileage_report"
android:textColor="#ffffff"
android:textSize="24px" />
</LinearLayout>
<LinearLayout
android:id="@+id/left_right_click_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20px"
android:orientation="horizontal">
<TextView
android:id="@+id/search_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="42px"
android:text="@string/search"
android:textColor="#ffffff"
android:textSize="26px" />
<Button
android:id="@+id/left_button1"
android:layout_width="80px"
android:layout_height="80px"
android:layout_marginLeft="26px"
android:background="@drawable/left_button" />
<TextView
android:id="@+id/time_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:text="2022"
android:textColor="#ffffff"
android:textSize="24px" />
<Button
android:id="@+id/left_button2"
android:layout_width="80px"
android:layout_height="80px"
android:layout_marginLeft="10px"
android:background="@drawable/right_button" />
<Button
android:id="@+id/left_button3"
android:layout_width="80px"
android:layout_height="80px"
android:layout_marginLeft="12px"
android:background="@drawable/left_button" />
<TextView
android:id="@+id/time_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:text="04"
android:textColor="#ffffff"
android:textSize="24px" />
<Button
android:id="@+id/left_button4"
android:layout_width="80px"
android:layout_height="80px"
android:layout_marginLeft="10px"
android:background="@drawable/right_button" />
<Button
android:id="@+id/custom_button"
android:layout_width="120px"
android:layout_height="60px"
android:layout_marginLeft="26px"
android:background="@drawable/button_change"
android:text="@string/customize"
android:textColor="#ffffff"
android:textSize="26px" />
</LinearLayout>
<LinearLayout
android:id="@+id/distance_line"
android:layout_width="match_parent"
android:layout_height="86px"
android:layout_marginTop="8px"
android:background="@mipmap/bg_gr1"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20px"
android:text="@string/date"
android:textColor="#c8c9d0"
android:textSize="26px" />
<TextView
android:id="@+id/worktime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60px"
android:text="@string/working_time"
android:textColor="#c8c9d0"
android:textSize="26px" />
<TextView
android:id="@+id/startdistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="62px"
android:text="@string/start_distance"
android:textColor="#c8c9d0"
android:textSize="26px" />
<TextView
android:id="@+id/enddistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="45px"
android:text="@string/end_distance"
android:textColor="#c8c9d0"
android:textSize="26px" />
<RelativeLayout
android:id="@+id/startgas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="48px"
android:gravity="center">
<TextView
android:id="@+id/end_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_gas_consume"
android:textColor="#c8c9d0"
android:textSize="26px" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/endgas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40px"
android:gravity="center">
<TextView
android:id="@+id/end_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/end_gas_consume"
android:textColor="#c8c9d0"
android:textSize="26px" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
long duration = System.currentTimeMillis() ± 24 * 60 * 60 * 1000
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(duration );
String nowDate = simpleDateFormat.format(date);
通过截取nowDate 可以取得想要的日期
使用Calendar封装一下,实现 获取前一天(月、年)、后一天(月、年)就行
/**
* 获取几天前或几天后的时间
*
* @param day
* @return
*/
public static Date getOtherDay(int day) {
Calendar now = Calendar.getInstance();
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
public static Date getDateBeforeYear(int year) {
return getDateBefore(Calendar.YEAR, year);
}
public static Date getDateBeforeMonth(int month) {
return getDateBefore(Calendar.MONTH, month);
}
public static Date getDateBeforeDay(int day) {
return getDateBefore(Calendar.DAY_OF_YEAR, day);
}
/**
* 获取几小时之前或之后的时间
*
* @param hour
* @return
*/
public static Date getDateBeforeHour(int hour) {
return getDateBefore(Calendar.HOUR, hour);
}
/**
* 获取几分钟之前或之后的时间
*
* @param min
* @return
*/
public static Date getDateBeforeMin(int min) {
return getDateBefore(Calendar.MINUTE, min);
}
public static Date getDateBeforeSec(int sec) {
return getDateBefore(Calendar.SECOND, sec);
}
public static Date getDateBefore(int field, int amount) {
Calendar calendar = Calendar.getInstance();
calendar.add(field, amount);
return calendar.getTime();
}
给图片设置一个点击监听 xxx.setOnclickListenter(),然后在回调里面写具体的日期加减逻辑。