记账本开发进度2024.1.20

zbl040721 / 2024-02-29 / 原文

添加账目记录实现:

简述:

日期选择采用CalendarView控件,时间选择采用TimePicker控件,然后通过switch控件控制其VISIBLE和GONE属性,类型通过PopUpWindows痰喘显示,标签通过SharePreferences进行传递。最后插入SQLite数据库中。

实现:

获取日期:

因为获取时间的日历控件的月份比实际少一个月,因此需要把打分加上一。然后将获取的年月日字符串数据转化为Date格式,最后将Date格式转为当时的星期

字符串时间转Date:

public static Date getStringToDate(String str){
mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
try {
Date date = mSimpleDateFormat.parse(str);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}

Date转星期:

public static String getWeekOfDate(Date date) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);

int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}

将获取的日期、星期进行保存:

/**
* 显示选择的年月日,并将选择的年月日转为星期
*/
private void getDate() {
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
month = month + 1;
CNDate = year + "年" + month + "月" + dayOfMonth + "日";
String date = year + "-" + month + "-" + dayOfMonth;
Log.d(TAG, "date=" + date);
Log.d(TAG, "CNdate=" + CNDate);
//string日期转date日期,在转为星期
String week = DateUtils.getWeekOfDate(DateUtils.getStringToDate(date));
Log.d(TAG, "week=" + week);
SelectDate.setText(CNDate + " " + week);


}
});
}

获取时间:

直接堆TimePicker控件进行实践监听,然后将获取的时间进行保存

private void getTime() {
mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
String time = hourOfDay + ":" + minute;
SelectTime.setText(time);
Log.d(TAG, time);
}
});
}

Switch控制显示和隐藏:

更改Switch样式:

建立thumb.xml和track.xml两个选择器

thumb.xml如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/open_thumb"/>
<item android:drawable="@drawable/shut_thumb"/>
</selector>

track.xml如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/open_track"/>
<item android:drawable="@drawable/shut_track"/>
</selector>

然后分别建立两个选择器的不同状态下的效果文件

open_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- 高度40 -->
<size android:height="30dp" android:width="30dp"/>
<!-- 圆角弧度 20 -->
<corners android:radius="15dp"/>
<!-- 变化率 -->

<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<!--<stroke android:width="1dp"-->
<!-- android:color="#2196F3"/>-->
</shape>

shut_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size android:height="30dp" android:width="30dp"/>
<!-- 圆角弧度 20 -->
<corners android:radius="15dp"/>
<!-- 变化率 -->
<gradient
android:endColor="#eeeeee"
android:startColor="#eeeeee" />
<stroke android:width="1dp"
android:color="#A8A7A7"/>

</shape>

open_track.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<!-- 高度30 此处设置宽度无效-->
<size android:height="30dp"/>
<!-- 圆角弧度 15 -->
<corners android:radius="15dp"/>
<!-- 变化率 定义从左到右的颜色不变 -->
<solid android:color="#07ED7D"/>

</shape>

shut_track.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:height="30dp" android:width="30dp"/>
<corners android:radius="15dp"/>
<gradient android:startColor="#eeeeee"
android:endColor="#eeeeee"/>
<stroke android:width="1dp"
android:color="#A8A7A7"/>
</shape>

最后应用于switch效果如下:

<Switch
android:id="@+id/DateSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:thumb="@drawable/thumb"
android:track="@drawable/track" />