android 仿预订日历时间选择(如去哪儿,携程时间选择)

马上就要下班,随便写个博客,废话不多说,直接看效果

android 仿预订日历时间选择(如去哪儿,携程时间选择)_第1张图片


看标题就知道了,一个日历选择,类似于去哪儿,携程,酒店预订功能

调用方法

package com.fly.caldroid;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;

import com.wz.caldroid.CalendarCellDecorator;
import com.wz.caldroid.CalendarPickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class CaldroidActivity extends Activity {
    private CalendarPickerView calendar;
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.calendar_activity);

        Bundle myBundle = getIntent().getExtras();
        long seleteTime  = myBundle.getLong("selete_time");
        final Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.MONTH, 3);

        final Calendar lastYear = Calendar.getInstance();
        lastYear.add(Calendar.MONTH, 0);

        calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
        Calendar today = Calendar.getInstance();
        ArrayList dates = new ArrayList();
        if (seleteTime>0){
            Date d1=new Date(seleteTime);
            dates.add(d1);
        }else{
            dates.add(today.getTime());
        }

        calendar.setDecorators(Collections.emptyList());
        calendar.init(lastYear.getTime(), nextYear.getTime()) //
                .inMode(CalendarPickerView.SelectionMode.MULTIPLE) //
                .withSelectedDate(dates.get(0));
        initButtonListeners();
    }



    private void initButtonListeners() {
        calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
                Intent intent = new Intent();
                intent.putExtra("SELETE_DATA_TIME", calendar.getSelectedDate().getTime());
                setResult(2, intent);
                finish();
            }

            @Override
            public void onDateUnselected(Date date) {

            }
        });

        View titlebar_img_back=findViewById(R.id.titlebar_img_back);
        titlebar_img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }



    @Override public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}

layout======

calendar_activity

xml version="1.0" encoding="utf-8"?>
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
            android:id="@+id/title_content_layout"
        android:layout_width="match_parent"
        android:layout_height="47.5dp"
        android:background="@color/big_red">

                    android:id="@+id/titlebar_text_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/titlebar_img_back"
            android:maxEms="8"
            android:singleLine="true"
            android:text="选择日历"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:visibility="visible" />

                    android:id="@+id/titlebar_img_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:padding="5dp"
            android:src="@drawable/back_ic"
            android:visibility="visible" />
    

            android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp"
        android:scrollbarStyle="outsideOverlay"
        android:clipToPadding="false"
        android:background="#FFFFFF"
        />


在textview上显示时间

package com.fly.caldroid;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private long seleteTime = 0;
    private TextView time_choice_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        time_choice_view = (TextView) findViewById(R.id.time_choice_view);
        time_choice_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putLong("selete_time", seleteTime);
                intent.putExtras(bundle);
                intent.setClass(MainActivity.this, CaldroidActivity.class);
                startActivityForResult(intent, 5);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 5) {
            if (resultCode == 2) {
                seleteTime = data.getLongExtra("SELETE_DATA_TIME", 0);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date d1 = new Date(seleteTime);
                String t1 = format.format(d1);
                if (seleteTime > 0) {
                    time_choice_view.setText(t1);
                } else {
                    return;
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
 
  
最后直接贴上demo下载地址
http://download.csdn.net/detail/mingzhnglei/9575733
 
  
有什么问题了  可以相互讨论



你可能感兴趣的:(android)