Android自定義實(shí)現(xiàn)日歷控件
本文實(shí)例為大家分享了Android自定義實(shí)現(xiàn)日歷控件的具體代碼,供大家參考,具體內(nèi)容如下
1. Calendar類
2. 布局
創(chuàng)建calendar_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="20sp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/titleRl"
android:layout_width="match_parent"
android:layout_height="30dp">
<TextView
android:id="@+id/lastTv"
android:text="上一月"
android:layout_alignParentLeft="true"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="30dp"></TextView>
<TextView
android:id="@+id/monthTv"
android:text="十一月"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="30dp"></TextView>
<TextView
android:id="@+id/nextTv"
android:text="下一月"
android:gravity="center"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="30dp"></TextView>
</RelativeLayout>
<LinearLayout
android:id="@+id/weekLl"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/Tv7"
android:text="日"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"></TextView>
<TextView
android:id="@+id/Tv1"
android:text="一"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"></TextView>
<TextView
android:id="@+id/Tv2"
android:text="二"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"></TextView>
<TextView
android:id="@+id/Tv3"
android:text="三"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"></TextView>
<TextView
android:id="@+id/Tv4"
android:text="四"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"></TextView>
<TextView
android:id="@+id/Tv5"
android:text="五"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"></TextView>
<TextView
android:id="@+id/Tv6"
android:text="六"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="30dp"></TextView>
</LinearLayout>
<GridView
android:id="@+id/calendarCv"
android:numColumns="7"
android:layout_width="match_parent"
android:layout_height="match_parent"></GridView>
</LinearLayout>
創(chuàng)建item_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/itemTv"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</LinearLayout>
在activity_main.xml中
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.aye.newcalendar.NewCalendar
android:id="@+id/calendarNc"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.aye.newcalendar.NewCalendar>
</androidx.constraintlayout.widget.ConstraintLayout>
3. 業(yè)務(wù)處理
創(chuàng)建NewCalendar類,繼承LinearLayout
public class NewCalendar extends LinearLayout {
private TextView lastTv,nextTv,dateTv;
private GridView calendarGv;
private Calendar calendar=Calendar.getInstance(); //日歷控件初始化
//重寫三個(gè)構(gòu)造方法
public NewCalendar(Context context) {
super(context);
}
public NewCalendar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initControl(context); //綁定控件
}
public NewCalendar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initControl(context); //綁定控件
}
private void initControl(Context context){
bindControl(context); //綁定控件
bindControlEvent(); //綁定控件事件
}
//綁定控件事件方法
private void bindControlEvent() {
renderCalendar();
//“下一月”點(diǎn)擊事件
nextTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.MONTH,+1); //月份+1
renderCalendar();
}
});
//“上一個(gè)”點(diǎn)擊事件
lastTv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calendar.add(Calendar.MONTH,-1); //月份-1
renderCalendar();
}
});
}
private void renderCalendar() {
SimpleDateFormat sdf = new SimpleDateFormat("MMM yyy"); //日期格式化
dateTv.setText(sdf.format(calendar.getTime())); //設(shè)置月份
ArrayList<Date> cells = new ArrayList<>();
Calendar calendar1 = (Calendar) calendar.clone(); //克隆日歷對(duì)象
calendar1.set(Calendar.DAY_OF_MONTH, 1); //置于當(dāng)月第一天;
int prevDays = calendar1.get(Calendar.DAY_OF_WEEK) - 1; //獲取上個(gè)月最后一天是星期幾
calendar1.add(Calendar.DAY_OF_MONTH, -prevDays); //第一天
int maxCount = 6 * 7; //設(shè)置每個(gè)月最大天數(shù)
//循環(huán)存入集合中
while (cells.size() < maxCount) {
cells.add(calendar1.getTime());
calendar1.add(Calendar.DAY_OF_MONTH, 1); //日期+1
}
//設(shè)置適配器
calendarGv.setAdapter(new CalendarAdapter(getContext(),cells));
}
//適配器
private class CalendarAdapter extends ArrayAdapter<Date>{
LayoutInflater layoutInflater;
public CalendarAdapter(@NonNull Context context,ArrayList<Date> days) {
super(context, R.layout.item_layout,days);
layoutInflater=LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Date date=getItem(position);
ViewHolder viewHolder;
if (convertView==null){ //初始化綁定
convertView=layoutInflater.inflate(R.layout.item_layout,parent,false);
viewHolder=new ViewHolder();
viewHolder.itemTv=convertView.findViewById(R.id.itemTv);
convertView.setTag(viewHolder);
}
viewHolder= (ViewHolder) convertView.getTag();
int day=date.getDate();
viewHolder.itemTv.setText(String.valueOf(day)); //賦值
return convertView;
}
class ViewHolder{
TextView itemTv;
}
}
private void bindControl(Context context) {
LayoutInflater inflater=LayoutInflater.from(context);
inflater.inflate(R.layout.calendar_layout,this);
lastTv=findViewById(R.id.lastTv);
nextTv=findViewById(R.id.nextTv);
dateTv=findViewById(R.id.dateTv);
calendarGv=findViewById(R.id.calendarGv);
}
}
3. 定制UI
在適配器getView()方法中,個(gè)性化日歷界面
Date now=new Date();
Boolean isTheSameMonth=false; //是否與當(dāng)前月份相同
//判斷顯示的日期月份與當(dāng)前月份相同
if (date.getMonth()==now.getMonth()) { //月份相同
isTheSameMonth = true;
}
//若顯示的日期月份與當(dāng)前月份相同,則設(shè)置字體顏色是黑色
if (isTheSameMonth) {
viewHolder.itemTv.setTextColor(Color.parseColor("#000000"));
}
//設(shè)置當(dāng)前日期字體為紅色
if (now.getDate()==date.getDate()&&now.getMonth()==date.getMonth()&&now.getYear()==date.getYear()){
viewHolder.itemTv.setTextColor(Color.parseColor("#ff0000"));
}
4. 事件監(jiān)聽
在NewCalendar中,首先編寫長(zhǎng)按事件的接口,然后設(shè)置適配器點(diǎn)擊事件
//長(zhǎng)按事件接口
public interface NewCalendarListener{
void onItemClick(Date date);
}
//適配器長(zhǎng)按事件
calendarGv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (listener==null){
return false;
}else {
//獲取長(zhǎng)按的位置,傳入onItemClick方法中
listener.onItemClick((Date) parent.getItemAtPosition(position));
return true;
}
}
});
在MainActivity中,實(shí)現(xiàn)長(zhǎng)按事件接口并重寫方法,實(shí)現(xiàn)長(zhǎng)按某個(gè)日期彈出Toast顯示當(dāng)前長(zhǎng)按日期。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NewCalendar calendar=findViewById(R.id.calendarNc);
calendar.listener=this;
}
//MainActivity實(shí)現(xiàn)長(zhǎng)按事件接口
@Override
public void onItemClick(Date date) {
DateFormat df= SimpleDateFormat.getDateInstance();
Toast.makeText(this,df.format(date),Toast.LENGTH_SHORT).show();
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 動(dòng)畫之RotateAnimation應(yīng)用詳解
本節(jié)講解旋轉(zhuǎn)動(dòng)畫效果RotateAnimation方法的應(yīng)用,有需要的朋友可以參考下2012-12-12
Android沉浸式狀態(tài)欄實(shí)現(xiàn)
這篇文章主要介紹了Android沉浸式狀態(tài)欄實(shí)現(xiàn),即一體化狀態(tài)欄實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-01-01
Android?Jetpack庫(kù)剖析之LiveData組件篇
LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來(lái)使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話說(shuō),LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)2022-07-07
Kotlin引用其他xml的view對(duì)象過(guò)程詳解
這篇文章主要介紹了Kotlin中如何引用其他xml中的view對(duì)象,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02
Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請(qǐng)求
本篇文章主要介紹了深入淺出RxJava+Retrofit+OkHttp網(wǎng)絡(luò)請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Android?Flutter實(shí)現(xiàn)頁(yè)面切換轉(zhuǎn)場(chǎng)動(dòng)畫效果
Hero組件非常適合從列表、概覽頁(yè)切換到詳情頁(yè)轉(zhuǎn)場(chǎng)動(dòng)畫場(chǎng)合。本文將利用Hero組件制作一個(gè)簡(jiǎn)單的頁(yè)面切換轉(zhuǎn)場(chǎng)動(dòng)畫效果,感興趣的可以了解一下2022-06-06
Android之rk3588?開發(fā)環(huán)境準(zhǔn)備及問(wèn)題解決方法
這篇文章主要介紹了Android中的rk3588?開發(fā)環(huán)境準(zhǔn)備,本文給大家分享遇到的問(wèn)題及解決方法,本文給大家講解的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
Android Studio 3.0被調(diào)方法參數(shù)名提示的取消方法
這篇文章主要介紹了去掉android studio 3.0被調(diào)方法參數(shù)名提示的解決方法,在文章末尾給大家補(bǔ)充介紹了Android Studio 3.0 gradle提示太老的解決方法,非常不錯(cuò),需要的朋友可以參考下2017-11-11

