简单的时间选择器

首先界面弹出PopupWindow动画的实现,具体的代码如下

进入动画

<code class="hljs xml has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> ></span>

    <span class="hljs-tag"><<span class="hljs-title">translate
</span>        <span class="hljs-attribute">android:duration</span>=<span class="hljs-value">"500"</span>
        <span class="hljs-attribute">android:fromYDelta</span>=<span class="hljs-value">"100.0%p"</span>
        <span class="hljs-attribute">android:toYDelta</span>=<span class="hljs-value">"45"</span> /></span>

<span class="hljs-tag"></<span class="hljs-title">set</span>></span></code>
退出动画

<code class="hljs xml has-numbering"><span class="hljs-pi">?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag"><<span class="hljs-title">set</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span> ></span>

    <span class="hljs-tag"><<span class="hljs-title">translate
</span>        <span class="hljs-attribute">android:duration</span>=<span class="hljs-value">"500"</span>
        <span class="hljs-attribute">android:fromYDelta</span>=<span class="hljs-value">"0.0"</span>
        <span class="hljs-attribute">android:toYDelta</span>=<span class="hljs-value">"100.0%p"</span> /></span>

<span class="hljs-tag"></<span class="hljs-title">set</span>></span></code>
主要界面的内容

<code class="hljs java has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">View</span>.<span class="hljs-title">OnClickListener</span>{</span>
    <span class="hljs-keyword">private</span> TextView tv_house_time;
    <span class="hljs-keyword">private</span> TextView tv_center;
    <span class="hljs-keyword">private</span> WheelMain wheelMainDate;
    <span class="hljs-keyword">private</span> String beginTime;

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-comment">// TODO Auto-generated method stub</span>
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initEvent</span>() {
        tv_house_time.setOnClickListener(<span class="hljs-keyword">this</span>);
    }

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initView</span>() {
        tv_house_time = (TextView) findViewById(R.id.tv_house_time);
        tv_center = (TextView) findViewById(R.id.tv_center);
    }

    <span class="hljs-keyword">private</span> java.text.DateFormat dateFormat = <span class="hljs-keyword">new</span> SimpleDateFormat(<span class="hljs-string">"yyyy-MM-dd"</span>);
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">showBottoPopupWindow</span>() {
        WindowManager manager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = manager.getDefaultDisplay();
        DisplayMetrics outMetrics = <span class="hljs-keyword">new</span> DisplayMetrics();
        defaultDisplay.getMetrics(outMetrics);
        <span class="hljs-keyword">int</span> width = outMetrics.widthPixels;
                View menuView = LayoutInflater.from(<span class="hljs-keyword">this</span>).inflate(R.layout.show_popup_window,<span class="hljs-keyword">null</span>);
        <span class="hljs-keyword">final</span> PopupWindow mPopupWindow = <span class="hljs-keyword">new</span> PopupWindow(menuView, (<span class="hljs-keyword">int</span>)(width*<span class="hljs-number">0.8</span>),
                ActionBar.LayoutParams.WRAP_CONTENT);
        ScreenInfo screenInfoDate = <span class="hljs-keyword">new</span> ScreenInfo(<span class="hljs-keyword">this</span>);
        wheelMainDate = <span class="hljs-keyword">new</span> WheelMain(menuView, <span class="hljs-keyword">true</span>);
        wheelMainDate.screenheight = screenInfoDate.getHeight();
        String time = DateUtils.currentMonth().toString();
        Calendar calendar = Calendar.getInstance();
        <span class="hljs-keyword">if</span> (JudgeDate.isDate(time, <span class="hljs-string">"yyyy-MM-DD"</span>)) {
            <span class="hljs-keyword">try</span> {
                calendar.setTime(<span class="hljs-keyword">new</span> Date(time));
            } <span class="hljs-keyword">catch</span> (Exception e) {
                e.printStackTrace();
            }
        }
        <span class="hljs-keyword">int</span> year = calendar.get(Calendar.YEAR);
        <span class="hljs-keyword">int</span> month = calendar.get(Calendar.MONTH);
        <span class="hljs-keyword">int</span> day = calendar.get(Calendar.DAY_OF_MONTH);
        <span class="hljs-keyword">int</span> hours = calendar.get(Calendar.HOUR_OF_DAY);
        <span class="hljs-keyword">int</span> minute = calendar.get(Calendar.MINUTE);
        wheelMainDate.initDateTimePicker(year, month, day, hours,minute);
        <span class="hljs-keyword">final</span> String currentTime = wheelMainDate.getTime().toString();
        mPopupWindow.setAnimationStyle(R.style.AnimationPreview);
        mPopupWindow.setTouchable(<span class="hljs-keyword">true</span>);
        mPopupWindow.setFocusable(<span class="hljs-keyword">true</span>);
        mPopupWindow.setBackgroundDrawable(<span class="hljs-keyword">new</span> BitmapDrawable());
        mPopupWindow.showAtLocation(tv_center, Gravity.CENTER, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);
        mPopupWindow.setOnDismissListener(<span class="hljs-keyword">new</span> poponDismissListener());
        backgroundAlpha(<span class="hljs-number">0.6</span>f);
        TextView tv_cancle = (TextView) menuView.findViewById(R.id.tv_cancle);
        TextView tv_ensure = (TextView) menuView.findViewById(R.id.tv_ensure);
        TextView tv_pop_title = (TextView) menuView.findViewById(R.id.tv_pop_title);
        tv_pop_title.setText(<span class="hljs-string">"选择起始时间"</span>);
        tv_cancle.setOnClickListener(<span class="hljs-keyword">new</span> View.OnClickListener() {
            <span class="hljs-annotation">@Override</span>
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClick</span>(View arg0) {
                mPopupWindow.dismiss();
                backgroundAlpha(<span class="hljs-number">1</span>f);
            }
        });
        tv_ensure.setOnClickListener(<span class="hljs-keyword">new</span> View.OnClickListener() {

            <span class="hljs-annotation">@Override</span>
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClick</span>(View arg0) {
                beginTime = wheelMainDate.getTime().toString();
                <span class="hljs-keyword">try</span> {
                    Date begin = dateFormat.parse(currentTime);
                    Date end = dateFormat.parse(beginTime);
                    tv_house_time.setText(DateUtils.currentTimeDeatil(begin));
                } <span class="hljs-keyword">catch</span> (ParseException e) {
                    e.printStackTrace();
                }
                mPopupWindow.dismiss();
                backgroundAlpha(<span class="hljs-number">1</span>f);
            }
        });
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">backgroundAlpha</span>(<span class="hljs-keyword">float</span> bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClick</span>(View v) {
        <span class="hljs-keyword">switch</span> (v.getId()){
            <span class="hljs-keyword">case</span> R.id.tv_house_time:
                showBottoPopupWindow();
                <span class="hljs-keyword">break</span>;
        }
    }

    class poponDismissListener implements PopupWindow.OnDismissListener {
        <span class="hljs-annotation">@Override</span>
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onDismiss</span>() {
            backgroundAlpha(<span class="hljs-number">1</span>f);
        }

    }
}</code>
布局内容的

<code class="hljs avrasm has-numbering"><LinearLayout xmlns:android=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
    android:id=<span class="hljs-string">"@+id/rel_select"</span>
    android:layout_width=<span class="hljs-string">"wrap_content"</span>
    android:layout_height=<span class="hljs-string">"wrap_content"</span>
    android:layout_centerInParent=<span class="hljs-string">"true"</span>
    android:layout_margin=<span class="hljs-string">"10dp"</span>
    android:background=<span class="hljs-string">"@drawable/border_circle_radius"</span>
    android:orientation=<span class="hljs-string">"vertical"</span> >
<TextView
    android:background=<span class="hljs-string">"#2F0F9980"</span>
    android:padding=<span class="hljs-string">"10dp"</span>
    android:id=<span class="hljs-string">"@+id/tv_pop_title"</span>
    android:textSize=<span class="hljs-string">"18sp"</span>
    android:gravity=<span class="hljs-string">"center"</span>
    android:textColor=<span class="hljs-string">"#301616"</span>
    android:layout_width=<span class="hljs-string">"match_parent"</span>
    android:layout_height=<span class="hljs-string">"wrap_content"</span> />
    <LinearLayout
        android:id=<span class="hljs-string">"@+id/timePicker1"</span>
        android:layout_width=<span class="hljs-string">"match_parent"</span>
        android:layout_height=<span class="hljs-string">"wrap_content"</span>
        android:orientation=<span class="hljs-string">"horizontal"</span> >
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.WheelView</span>
            android:id=<span class="hljs-string">"@+id/year"</span>
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_weight=<span class="hljs-string">"1"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span> />
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.DashedLineView</span>
            android:layout_width=<span class="hljs-string">"1dp"</span>
            android:gravity=<span class="hljs-string">"center_vertical"</span>
            android:layout_gravity=<span class="hljs-string">"center"</span>
            android:background=<span class="hljs-string">"@drawable/dotted_line"</span>
            android:layout_height=<span class="hljs-string">"match_parent"</span> />
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.WheelView</span>
            android:id=<span class="hljs-string">"@+id/month"</span>
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_weight=<span class="hljs-string">"1"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span> />
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.DashedLineView</span>
            android:layout_width=<span class="hljs-string">"1dp"</span>
            android:gravity=<span class="hljs-string">"center_vertical"</span>
            android:layout_gravity=<span class="hljs-string">"center"</span>
            android:background=<span class="hljs-string">"@drawable/dotted_line"</span>
            android:layout_height=<span class="hljs-string">"match_parent"</span> />
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.WheelView</span>
            android:id=<span class="hljs-string">"@+id/day"</span>
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_weight=<span class="hljs-string">"1"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span> />
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.DashedLineView</span>
            android:layout_width=<span class="hljs-string">"1dp"</span>
            android:background=<span class="hljs-string">"@drawable/dotted_line"</span>
            android:layout_height=<span class="hljs-string">"match_parent"</span> />
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.WheelView</span>
            android:id=<span class="hljs-string">"@+id/hour"</span>
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_weight=<span class="hljs-string">"1"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span> />
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.DashedLineView</span>
            android:layout_width=<span class="hljs-string">"1dp"</span>
            android:background=<span class="hljs-string">"@drawable/dotted_line"</span>
            android:layout_height=<span class="hljs-string">"match_parent"</span> />
        <liuyongxiang<span class="hljs-preprocessor">.robert</span><span class="hljs-preprocessor">.com</span><span class="hljs-preprocessor">.testtime</span><span class="hljs-preprocessor">.wheelview</span><span class="hljs-preprocessor">.WheelView</span>
            android:id=<span class="hljs-string">"@+id/mins"</span>
            android:layout_width=<span class="hljs-string">"wrap_content"</span>
            android:layout_weight=<span class="hljs-string">"1"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span> />
    </LinearLayout>
    <LinearLayout
        android:layout_width=<span class="hljs-string">"match_parent"</span>
        android:layout_height=<span class="hljs-string">"wrap_content"</span>
        android:background=<span class="hljs-string">"#2F0F9980"</span>
        android:padding=<span class="hljs-string">"10dp"</span>
        android:orientation=<span class="hljs-string">"horizontal"</span> >

        <TextView
            android:id=<span class="hljs-string">"@+id/tv_cancle"</span>
            android:layout_width=<span class="hljs-string">"0dp"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:gravity=<span class="hljs-string">"center"</span>
            android:paddingLeft=<span class="hljs-string">"20dp"</span>
            android:paddingRight=<span class="hljs-string">"20dp"</span>
            android:padding=<span class="hljs-string">"5dp"</span>
            android:textSize=<span class="hljs-string">"18sp"</span>
            android:textColor=<span class="hljs-string">"#ff0000"</span>
            android:layout_weight=<span class="hljs-string">"1"</span>
            android:background=<span class="hljs-string">"@drawable/btn_pop"</span>
            android:text=<span class="hljs-string">"取消"</span> />

        <TextView
            android:layout_width=<span class="hljs-string">"0dp"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:layout_weight=<span class="hljs-string">"3"</span> />

        <TextView
            android:id=<span class="hljs-string">"@+id/tv_ensure"</span>
            android:layout_width=<span class="hljs-string">"0dp"</span>
            android:layout_height=<span class="hljs-string">"wrap_content"</span>
            android:gravity=<span class="hljs-string">"center"</span>
            android:textColor=<span class="hljs-string">"#414341"</span>
            android:padding=<span class="hljs-string">"5dp"</span>
            android:paddingLeft=<span class="hljs-string">"20dp"</span>
            android:paddingRight=<span class="hljs-string">"20dp"</span>
            android:layout_weight=<span class="hljs-string">"1"</span>
            android:textSize=<span class="hljs-string">"18sp"</span>
            android:background=<span class="hljs-string">"@drawable/btn_pop"</span>
            android:text=<span class="hljs-string">"确定"</span> />
    </LinearLayout>


</LinearLayout></code>

请注意

MainActivity里面的显示时间的 tv_house_time.setText(DateUtils.currentTimeDeatil(begin));需要更改为
tv_house_time.setText(DateUtils.formateStringH(beginTime,DateUtils.yyyyMMddHHmm));否则现实的额时间为00:00
修改后的

wv_mins.setAdapter(adapter);
wv_mins.setCyclic(true);// 可循环滚动
wv_mins.setLabel(“分”);// 添加文字
int min = setMinute(m);
wv_mins.setCurrentItem(min);
更换为
wv_mins.setAdapter(new NumericWheelAdapter(
0, 59));
wv_mins.setCyclic(true);// 可循环滚动
wv_mins.setLabel(“分”);// 添加文字
wv_mins.setCurrentItem(m);
还需要将
int minute = Integer.valueOf(adapter.getItem(wv_mins.getCurrentItem()));
改为
int minute = wv_mins.getCurrentItem();
会将分钟更改为从0到59

如果不想要时间只想要年月日的话只需要
if (hasSelectTime) {
wv_hours.setVisibility(View.GONE);
wv_mins.setVisibility(View.GONE);

    } else {
        wv_hours.setVisibility(View.GONE);
        wv_mins.setVisibility(View.GONE);
        wv_day.setVisibility(View.GONE);
    }
    将这段代码放开就可以了还要将以下绿色区域内的代码去掉
还需要将 MainActivty里的如下代码
wheelMainDate.initDateTimePicker(year, month, day, hours,minute);
更改为
wheelMainDate.initDateTimePicker(year, month, day);
还有 wheelMain里的
if (!hasSelectTime) {
        sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
                .append(strMon).append("-")
                .append(strDay).append("  ").append(strHour).append(":").append(strMin);
    }else{
        sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
                .append(strMon).append("-")
                .append(strDay).append("  ").append(strHour).append(":").append(strMin);
    }

需要修改为
if (!hasSelectTime) {

        sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
                .append(strMon).append("-")
                .append(strDay);
    }else{
        sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
                .append(strMon).append("-")
                .append(strDay);
    }

实现效果如下图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值