滑轮选择,WheelView的简单使用

简单粗暴,直接上图


主要的代码解析很清楚

当然,并没有去写调用系统的时间,所以就随便复制点常量搞上了

package com.example.wheelviewtext;


import org.w3c.dom.Text;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends Activity {
	//定义常量数据
	private String[] Week={"周一","周二","周三","周四","周五","周六","周日"};
	private String[] Month={"1","2","3","4","5","6","7","8","9","10","11","12"};
	private String[] Day={"1","2","3","4","5","6","7","8","9","10"
						  ,"11","12","13","14","15","16","17","18","19","20"
						  ,"21","22","23","24","25","26","27","28","29","30","31"};
	private String[] Month_Day_Week=new String[365];
	private int Week_Count=3;private int k=0;
	private String[] Hour={"0","1","2","3","4","5","6","7","8","9","10","11"
							,"12","13","14","15","16","17","18","19","20","21","22","23"};
	private String[] Minute={"00","01","02","03","04","05","06","07","08","09"
							,"10","11","12","13","14","15","16","17","18","19"
							,"20","21","22","23","24","25","26","27","28","29"
							,"30","31","32","33","34","35","36","37","38","39"
							,"40","41","42","43","44","45","46","47","48","49"
							,"50","51","52","53","54","55","56","57","58","59"};
	//数据初始化
	public void init()
	{
		for(int i=0;i<12;i++)
		{

			for(int j=0;j<31;j++)
			{			
				//2月,暂不考虑闰年
				if(i==1&&j>27)
					break;
				if((i==3||i==5||i==8||i==10)&&j>29)
					break;
				Week_Count%=7;
				Month_Day_Week[k++]=new String(Month[i]+"月"+Day[j]+"日"+Week[Week_Count++]);
			}
		}
	}
	private Button btn;
	private TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState){
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化调用
        init();
        btn = (Button) findViewById(R.id.btn);
        text=(TextView) findViewById(R.id.text);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelectDialog(MainActivity.this, "日期选择", Month_Day_Week, Hour,Minute);
            }
        });
        
    }
    
    //布局及显示的自定义函数
    private void showSelectDialog(Context context, String title, final String[] Month_Day_Week, final String[] Hour,final String Minute[])
    {
    	//定义布局控件
    	AlertDialog dialog = new AlertDialog.Builder(context).create();
    	dialog.setTitle(title);
        LinearLayout llContent = new LinearLayout(context);
        llContent.setOrientation(LinearLayout.HORIZONTAL);
        
        //定义月日周的WheelView
        final WheelView wheelMonth = new WheelView(context);
        //画面显示个数
        wheelMonth.setVisibleItems(5);
        wheelMonth.setCyclic(true);
        /*	设置监听布局
       	   	第一个参数为对应为传入的数据,第二个参数为初始化显示字节最大长度
          	一个字为2字节,一个数字为1字节   以最大为标准
          	12月30日周日    4个字8字节,4个数字4字节 共12字节
         */
        wheelMonth.setAdapter(new ArrayWheelAdapter<String>(Month_Day_Week,12));
        
        //定义小时
        final WheelView wheelHour = new WheelView(context);
        wheelHour.setVisibleItems(5);
        wheelHour.setCyclic(true);
        wheelHour.setAdapter(new ArrayWheelAdapter<String>(Hour,2));
        
        //定义分钟
        final WheelView wheelMinute=new WheelView(context);
        wheelMinute.setVisibleItems(5);
        wheelMinute.setCyclic(true);
        wheelMinute.setAdapter(new ArrayWheelAdapter<String>(Minute,2));
        
        //月日周的显示容器
        LinearLayout.LayoutParams paramsMonth = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, (float) 2.0);
        paramsMonth.gravity = Gravity.LEFT;
        
        //时的显示容器
        LinearLayout.LayoutParams paramsHour = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, (float) 0.5);
        paramsHour.gravity = Gravity.CENTER;
        
        //分钟的显示容器
        LinearLayout.LayoutParams paramsMinute = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, (float) 0.5);
        paramsMinute.gravity = Gravity.RIGHT;
        
        //分别添加View进入各自的容器
        llContent.addView(wheelMonth, paramsMonth);
        llContent.addView(wheelHour, paramsHour);
        llContent.addView(wheelMinute,paramsMinute);
        
        
        //设置Month改变监听
        wheelMonth.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
            }
        });
        //小时的监听器
        wheelHour.addChangingListener(new OnWheelChangedListener() {			
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
			}
		});
        //分钟的监听器
        wheelMinute.addChangingListener(new OnWheelChangedListener() {
			
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub		
			}
		});
        
        //按钮触发事件
        dialog.setButton(AlertDialog.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int leftPosition = wheelMonth.getCurrentItem();
                String vMonth = Month_Day_Week[leftPosition];
                String vHour = Hour[wheelHour.getCurrentItem()];
                String vMinute=Minute[wheelMinute.getCurrentItem()];
                text.setText(vMonth + "-" + vHour+":"+vMinute);
                dialog.dismiss();
            }
        });
        dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        //总布局显示,对话框
        dialog.setView(llContent);
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }
    
    
    
    
    
}

(eclipse)代码:http://pan.baidu.com/s/1o6tGXlg



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值