Android 自定义日历控件

之前为了寻找项目需求:从底部弹出一个popupwindow,里面有一个能上下滑动的垂直的日历控件:

效果图如下:

技术分享


代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
  >

   <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ff424542"
        android:orientation="horizontal" 
        android:layout_above="@+id/bithday_layout"
        >
        
               <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:layout_centerVertical="true"
            android:textColor="@android:color/white"
            android:text="日期" />
	<Button
            android:id="@+id/cancel"
            android:layout_width="80dip"
            android:layout_height="wrap_content"     
            android:layout_alignParentLeft="true"
            android:background="@drawable/mm_title_btn_right"
            android:text="取消"
            android:textColor="@android:color/white"
            
             />
       



 

        <Button
            android:id="@+id/submit"
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@drawable/mm_title_act_btn"
            android:text="完成"
            android:textColor="@android:color/white" 
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>
     <RelativeLayout
          android:id="@+id/bithday_layout"
         android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="220dip"
        android:gravity="center"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="15dip"
            android:layout_height="220dip"
            android:gravity="center"
            android:orientation="horizontal" >

            <com.example.widget.WheelView
                android:id="@+id/year"
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1" />

            <com.example.widget.WheelView
                android:id="@+id/month"
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1" 
                
                />
            <com.example.widget.WheelView
                android:id="@+id/day"
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1" 
                
                />
        </LinearLayout>

       <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="220.0dip"
            android:layout_gravity="center"
            android:background="@drawable/com_ttshrk_view_scroll_picker_background" >
        </FrameLayout> 
    </RelativeLayout>
</LinearLayout>

package com.jiahui.activitygroup;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TabHost;

public class MainTabActivity extends TabActivity {
	FrameLayout fmpan;
	TabHost tabHost;
	ImageView image;
	FrameLayout fm;
	LayoutInflater inflater;
	private RadioButton tab_home, tab_second;
	public boolean isReverse = false;
	PopupWindow popup;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab);
		initView();
		fm.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (isReverse) {
					isReverse = false;
					popup.dismiss();
					image.setImageResource(R.drawable.toolbar_plus);
				} else {
					isReverse = true;
					image.setImageResource(R.drawable.toolbar_plusback);
					showWindow(fmpan);
				}
			}

		});

	}

	private void initView() {
		inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// View view=inflater.inflate(R.layout.tab,null);
		fmpan = (FrameLayout) findViewById(R.id.tab1);
		fm = (FrameLayout) findViewById(R.id.btn_ck);
		image = (ImageView) findViewById(R.id.image1);
		View view = inflater.inflate(R.layout.write_tab, null);
	}

	/**
	 * 60. * 显示 61. * 62. * @param parent 63.
	 */
	private void showWindow(View parent) {

		if (popup == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View view = layoutInflater.inflate(R.layout.write_tab, null);
			// 创建一个PopuWidow对象
			popup = new PopupWindow(view,
					LinearLayout.LayoutParams.MATCH_PARENT, 320);
		}
		// 使其聚集
		popup.setFocusable(true);
		// 设置允许在外点击消失
		popup.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
		popup.setBackgroundDrawable(new BitmapDrawable());
		popup.setTouchInterceptor(new OnTouchListener() {

			public boolean onTouch(View view, MotionEvent event) {

				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {

					isReverse = false;
					popup.dismiss();
					image.setImageResource(R.drawable.toolbar_plus);

					return true;

				}

				return false;
			}
		});

		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半
		int xPos = windowManager.getDefaultDisplay().getWidth() / 2
				- popup.getWidth() / 2;
		popup.showAsDropDown(parent, Gravity.CENTER, 0);
	}

}


源码下载地址:

自定义日历.jar



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。