Android日歷控件的實(shí)現(xiàn)方法
本文實(shí)例為大家分享了Android日歷控件的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
1、效果圖:
2、彈窗Dialog:SelectDateDialog:
public class SelectDateDialog {
private static final String TAG = "SelectDateDialog";
private Dialog dialog;
private TextView dateText;
private int selectYear, selectMonth;
private AppCompatActivity mContext;
private DateAdapter adapter;
private List<String> selWeekList = new ArrayList<>();
private List<DateBean> list = new ArrayList<>();
public SelectDateDialog builder(AppCompatActivity mContext, int year, int month) {
this.mContext = mContext;
this.selectYear = year;
this.selectMonth = month;
// 獲取Dialog布局
View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_date, null);
// 定義Dialog布局和參數(shù)
dialog = new Dialog(mContext, R.style.AlertDialogStyle);
dialog.setCanceledOnTouchOutside(false);//點(diǎn)擊外部是否取消
dialog.setCancelable(false);
dialog.setContentView(view);
Window window = dialog.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = (ScreenUtils.getScreenWidth(mContext));
// params.height = (int) (ScreenUtils.getScreenHeight(mContext) * 0.5);
window.setAttributes(params);
window.setGravity(Gravity.BOTTOM);
RecyclerView recycler = view.findViewById(R.id.recycler_select_date);
dateText = view.findViewById(R.id.date_text);
dateText.setText(year + "年" + month + "月");
//下個(gè)月
view.findViewById(R.id.next_month).setOnClickListener(view13 -> {
if (selectMonth > 11) {
selectYear = selectYear + 1;
selectMonth = 1;
} else {
selectMonth++;
}
showNewData(selectYear, selectMonth);
});
//上個(gè)月
view.findViewById(R.id.last_month).setOnClickListener(view14 -> {
if (selectMonth < 2) {
selectYear = selectYear - 1;
selectMonth = 12;
} else {
selectMonth--;
}
showNewData(selectYear, selectMonth);
});
list = DataUtils.getCalendar(year, month);
adapter = new DateAdapter(mContext, list);
GridLayoutManager manager = new GridLayoutManager(mContext, 7);
recycler.setLayoutManager(manager);
recycler.setAdapter(adapter);
//取消
view.findViewById(R.id.middle_cancel).setOnClickListener(view1 -> {
dialog.dismiss();
});
//確定
view.findViewById(R.id.middle_determine).setOnClickListener(view1 -> {
initSelect();
});
//設(shè)置選中當(dāng)天
adapter.setNowDay(year, month, DataUtils.getLastMonth(year, month));
return this;
}
private void showNewData(int year, int month) {
list = DataUtils.getCalendar(year, month);
//更新月數(shù)據(jù)
adapter.setList(list);
adapter.setNowDay(year , month, DataUtils.getLastMonth(year, month));
dateText.setText(year + "年" + month + "月");
}
/**
* 獲取選中的日期
*/
private void initSelect() {
selWeekList.clear();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isFlag()) {
//記錄數(shù)量
selWeekList.add(selectYear + "-" + selectMonth + "-" + list.get(i).getWeek());
}
}
Log.e(TAG, "initSelect: "+ list.size());
Log.e(TAG, "initSelect: "+ selWeekList.size());
if (selWeekList.size() == 0) {
Toast.makeText(mContext, "未選則日期", Toast.LENGTH_SHORT).show();
return;
}
dialog.dismiss();
listener.date(DataUtils.returnList(selWeekList));
Log.e(TAG, "initSelect: " + DataUtils.returnList(selWeekList));
}
public void show() {
if (dialog != null && (!dialog.isShowing())) {
dialog.show();
}
}
private OnDateListener listener;
public SelectDateDialog setListener(OnDateListener listener) {
this.listener = listener;
return this;
}
public interface OnDateListener {
void date(String selectDate);
}
}
ScreenUtils:
public class ScreenUtils {
public static int getScreenWidth(Context context) {
DisplayMetrics localDisplayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
return localDisplayMetrics.widthPixels;
}
}
DateBean:
public class DateBean {
private int week;
private boolean sign;
//0 上月 1本月 2下月
private int month;
private boolean isFlag;
public boolean isFlag() {
return isFlag;
}
public void setFlag(boolean flag) {
isFlag = flag;
}
public int getWeek() {
return week;
}
public void setWeek(int week) {
this.week = week;
}
public boolean isSign() {
return sign;
}
public void setSign(boolean sign) {
this.sign = sign;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public DateBean(int week, boolean sign, int month) {
this.week = week;
this.sign = sign;
this.month = month;
}
}
適配器:DateAdapter
public class DateAdapter extends CommonRecyclerAdapter<DateBean> {
private static final String TAG = "DateAdapter";
private Integer nowDay;
private int year;
private int month;
public DateAdapter(Context context, List<DateBean> list) {
super(context, R.layout.item_date, list);
}
public void setNowDay(int year, int month, int nowDay) {
this.nowDay = nowDay;
this.year = year;
this.month = month;
notifyDataSetChanged();
}
@Override
public void convert(RecyclerHolder holder, DateBean item, int position) {
TextView number = holder.getView(R.id.item_number);
number.setText(item.getWeek() + "");
//當(dāng)前年月等于切換年月時(shí)才選中當(dāng)天
if (position == nowDay) {
String date = year + "-" + month;
if (date.equals(DataUtils.timeInMillsTrasToDate(1))) {
number.setBackgroundResource(R.drawable.date_unsel_shape);
number.setTextColor(Color.WHITE);
}else {
number.setTextColor(Color.parseColor("#333333"));
}
} else {
number.setBackgroundResource(0);
number.setTextColor(Color.parseColor("#333333"));
if (item.getMonth() == 0 || item.getMonth() == 2) {
number.setTextColor(Color.parseColor("#CDCDCD"));
} else {
number.setTextColor(Color.parseColor("#333333"));
}
}
//點(diǎn)擊事件
number.setOnClickListener(view -> {
//本月可以點(diǎn)擊
int nowYear = Integer.parseInt(DataUtils.timeInMillsTrasToDate(2));
int nowMonth = Integer.parseInt(DataUtils.timeInMillsTrasToDate(3));
//只有在今天之后的日期才可以選中
if (year == nowYear) {
if (item.getMonth() == 1 && month == nowMonth && position > nowDay) {
onClick(item, number);
} else if (month > nowMonth) {
onClick(item, number);
}
} else if (year > nowYear) {
onClick(item, number);
}
});
}
private void onClick(DateBean item, TextView number) {
if (item.isFlag()) {
item.setFlag(false);
number.setBackgroundResource(0);
number.setTextColor(Color.parseColor("#333333"));
} else {
item.setFlag(true);
number.setBackgroundResource(R.drawable.date_sel_shape);
number.setTextColor(Color.WHITE);
}
}
}
注意:CommonRecyclerAdapter之前寫(xiě)過(guò),這里不再重復(fù)
數(shù)據(jù)源:DataUtils
public class DataUtils {
private static final String TAG = "DataUtils";
/**
* 日歷
*/
public static List<DateBean> getCalendar(int currentYear, int currentMonth) {
//實(shí)例化集合
List<DateBean> list = new ArrayList<>();
Calendar c = Calendar.getInstance();
c.clear();
/**
* 處理上個(gè)月月末
*/
if (currentMonth - 1 == 0) {
c.set(Calendar.YEAR, currentYear - 1);
c.set(Calendar.MONTH, 11);
} else {
c.set(Calendar.YEAR, currentYear);
c.set(Calendar.MONTH, (currentMonth - 2));
}
int last_sumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DATE, last_sumDays);
//得到一號(hào)星期幾
int weekDay = c.get(Calendar.DAY_OF_WEEK);
if (weekDay < 7) {
for (int i = weekDay - 1; i >= 0; i--) {
Integer date = new Integer(last_sumDays - i);
list.add(new DateBean(date, true, 0));
}
}
/**
* 處理當(dāng)前月
*/
c.clear();
c.set(Calendar.YEAR, currentYear);
c.set(Calendar.MONTH, currentMonth - 1);
int currentsumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i <= currentsumDays; i++) {
Integer date = new Integer(i);
list.add(new DateBean(date, true, 1));
}
/**
* 處理下個(gè)月月初
*/
c.clear();
if (currentMonth == 12) {
c.set(Calendar.YEAR, currentYear + 1);
c.set(Calendar.MONTH, 0);
} else {
c.set(Calendar.YEAR, currentYear);
c.set(Calendar.MONTH, currentMonth);
}
c.set(Calendar.DATE, 1);
int currentWeekDay = c.get(Calendar.DAY_OF_WEEK);
if (currentWeekDay > 2 && currentWeekDay < 8) {
for (int i = 1; i <= 8 - currentWeekDay; i++) {
list.add(new DateBean(i, true, 2));
}
}
return list;
}
/**
* 獲取上個(gè)月大小
*/
public static Integer getLastMonth(int currentYear, int currentMonth) {
//實(shí)例化集合
List<Integer> list = new ArrayList<>();
Calendar c = Calendar.getInstance();
c.clear();
/**
* 處理上個(gè)月月末
*/
if (currentMonth - 1 == 0) {
c.set(Calendar.YEAR, currentYear - 1);
c.set(Calendar.MONTH, 11);
} else {
c.set(Calendar.YEAR, currentYear);
c.set(Calendar.MONTH, (currentMonth - 2));
}
int last_sumDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DATE, last_sumDays);
//得到一號(hào)星期幾
int weekDay = c.get(Calendar.DAY_OF_WEEK);
if (weekDay < 7) {
for (int i = weekDay - 1; i >= 0; i--) {
list.add(i);
}
}
Log.e(TAG, "getLastMonth: " + Integer.parseInt(timeInMillsTrasToDate(0)));
return list.size() + Integer.parseInt(timeInMillsTrasToDate(0)) - 1;
}
/**
* list轉(zhuǎn)string字符串
*/
public static String returnList(List<String> list) {
if (null == list && list.size() == 0) {
return "";
} else {
//去除空格
String str = String.valueOf(list).replaceAll(" ", "");
return str.substring(1, str.length() - 1);
}
}
/**
* 時(shí)間轉(zhuǎn)換
*/
@TargetApi(Build.VERSION_CODES.N)
public static String timeInMillsTrasToDate(int formatType) {
DateFormat formatter = null;
switch (formatType) {
case 0:
formatter = new SimpleDateFormat("dd");
break;
case 1:
formatter = new SimpleDateFormat("yyyy-MM");
break;
case 2:
formatter = new SimpleDateFormat("yyyy");
break;
case 3:
formatter = new SimpleDateFormat("MM");
break;
}
Calendar calendar = Calendar.getInstance();
return formatter.format(calendar.getTime());
}
}
使用:DateActivity
public class DateActivity extends AppCompatActivity {
private Button openDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
openDate = this.findViewById(R.id.open_date);
openDate.setOnClickListener(view -> {
showDateDialog();
});
}
private void showDateDialog() {
int year = Integer.parseInt(DataUtils.timeInMillsTrasToDate(2));
int month = Integer.parseInt(DataUtils.timeInMillsTrasToDate(3));
new SelectDateDialog().builder(this, year, month)
.setListener(selectDate -> openDate.setText(selectDate)).show();
}
}
對(duì)應(yīng)布局:activity_date
<?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"> <Button android:id="@+id/open_date" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="打開(kāi)日歷" /> </LinearLayout>
2、資源文件:
date_sel_shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="false"> <solid android:color="@color/colorAccent" /> <!--<stroke android:width="1dp" android:color="@color/white"/>--> <size android:width="30dp" android:height="30dp" /> </shape>
date_unsel_shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" android:useLevel="false"> <solid android:color="@color/colorPrimary" /> <!--<stroke android:width="1dp" android:color="@color/white"/>--> <size android:width="30dp" android:height="30dp" /> </shape>
dialog_shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp" /> <solid android:color="#ffffff" /> </shape>
item_date.xml:
<?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="wrap_content" android:gravity="center" android:padding="5dp"> <TextView android:id="@+id/item_number" android:layout_width="35dp" android:layout_height="35dp" android:gravity="center" android:text="10" android:textSize="15sp" /> </LinearLayout>
顏色:
<color name="colorPrimary">#008577</color> <color name="colorPrimaryDark">#00574B</color> <color name="colorAccent">#D81B60</color>
樣式:
<style name="AlertDialogStyle" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowFrame">@null</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> </style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android自定義View實(shí)現(xiàn)炫酷進(jìn)度條
欄 目:Android
下一篇:Android通過(guò)ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能
本文標(biāo)題:Android日歷控件的實(shí)現(xiàn)方法
本文地址:http://www.jygsgssxh.com/a1/Android/9111.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開(kāi)發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery


