android滑轮选择控件——whell

具体显示效果如图所示

android滑轮选择控件——whell_第1张图片

main.xml如下:


    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


            android:id="@+id/btn_showPopuWindow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/showPopuWindow" />

popuwindow.xml 如下:




    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_marginTop="12dp"
    android:orientation="vertical"
    android:background="@drawable/layout_bg">
            android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:paddingTop="30dp">
                            android:text="年份" 
                android:gravity="center_horizontal"
           android:layout_height="wrap_content"
           android:layout_width="71dp"/>
                  android:text="月"
           android:gravity="center_horizontal"
           android:layout_height="wrap_content"
           android:layout_width="40dp"/>
                  android:text="日"
           android:gravity="center_horizontal"
           android:layout_height="wrap_content"
           android:layout_width="40dp"/>
                  android:text="小时"
           android:gravity="center_horizontal"
           android:layout_height="wrap_content"
           android:layout_width="65dp"/>
                  android:text="分"
           android:gravity="center_horizontal"
           android:layout_height="wrap_content"
           android:layout_width="40dp"/>
                  android:text="秒"
           android:gravity="center_horizontal"
           android:layout_height="wrap_content"
           android:layout_width="40dp"/>
       

    
            android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:paddingTop="10dp">
        
                    android:layout_height="wrap_content"
            android:layout_width="71dp"/>
                    android:layout_height="wrap_content"
            android:layout_width="40dp"/>
                    android:layout_height="wrap_content"
            android:layout_width="40dp"/>
                    android:layout_height="wrap_content"
            android:layout_width="65dp"/>
                    android:layout_height="wrap_content"
            android:layout_width="40dp"/>
                    android:layout_height="wrap_content"
            android:layout_width="40dp"/>                
   

       android:id="@+id/start"
   android:text="@string/start"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:layout_gravity="center"
   android:background="@android:drawable/btn_default_small"
android:layout_marginTop="10dp"    
   />
            android:id="@+id/testtext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

MyPopuwindowActivity.java如下:

package hb.android.popuwindow;


import java.util.Calendar;


import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import kankan.wheel.widget.adapters.NumericWheelAdapter;


import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;


public class MyPopuwindowActivity extends Activity {
//弹出的window
PopupWindow popuWindow;
//点击弹出popuwindow
Button show;
//点击消失popuwindow
Button start;


private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化show Button
show = (Button) findViewById(R.id.btn_showPopuWindow);
show.setOnClickListener(new MyPopuWindowsListener());
//初始化popuwindow
View popuView = LayoutInflater.from(MyPopuwindowActivity.this).inflate(
R.layout.popuwindow, null);
//初始化start Button
start = (Button) popuView.findViewById(R.id.start);
start.setOnClickListener(new MyPopuWindowsListener());
//设置popuwindow的一些参数
popuWindow = new PopupWindow(popuView,
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popuWindow.setFocusable(false);

Calendar calendar = Calendar.getInstance();
//初始化年月日,时分秒whellview控件
final WheelView year = (WheelView) popuView.findViewById(R.id.myyear);
final WheelView month = (WheelView) popuView.findViewById(R.id.mymonth);
        final WheelView day = (WheelView) popuView.findViewById(R.id.myday);
        final WheelView hour = (WheelView) popuView.findViewById(R.id.myhour);
        final WheelView minute = (WheelView) popuView.findViewById(R.id.myminute);
        final WheelView second = (WheelView) popuView.findViewById(R.id.mysecond);
        //显示选择的时间结果
        text = (TextView) popuView.findViewById(R.id.testtext);
        //滑动监听执行方法
        OnWheelChangedListener listener = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
            //闰年与月份,月份与日的时间联动
            updateDayOfMonth(day, 2013+year.getCurrentItem(), 1+month.getCurrentItem());
            //滑动whellview时更新显示文本的内容
            updateDateTime(year,month,day,hour,minute,second);
            }
        };
        //初始化年月日,时分秒whellview控件,添加适配器和当前显示时间
        int curYear = calendar.get(Calendar.YEAR);
        //数字适配器
        year.setViewAdapter(new DateNumericAdapter(this, curYear, curYear + 10, 0));
        year.setCurrentItem(curYear);
        
        int curMonth = calendar.get(Calendar.MONTH);
        String months[] = new String[] {"1", "2", "3", "4", "5",
                "6", "7", "8", "9", "10", "11", "12"};
        //自定义适配器
        month.setViewAdapter(new DateArrayAdapter(this, months, curMonth));
        month.setCurrentItem(curMonth);
        
        int curDay = calendar.get(Calendar.DAY_OF_MONTH)-1;
        day.setViewAdapter(new DateNumericAdapter(this, 1, 31, curDay));
        day.setCurrentItem(curDay);
        
        int curHour = calendar.get(Calendar.HOUR_OF_DAY);
        hour.setViewAdapter(new DateNumericAdapter(this, 0, 23, curHour));
        hour.setCurrentItem(curHour);
        
        int curMinute = calendar.get(Calendar.MINUTE);
        //时间格式化适配器
        minute.setViewAdapter(new TimeNumericAdapter(this, 0, 59, curMinute, "%02d"));
        minute.setCurrentItem(curMinute);
        
//        int curSecond = calendar.get(Calendar.SECOND);
        second.setViewAdapter(new TimeNumericAdapter(this, 0, 59, 0, "%02d"));
        second.setCurrentItem(0);
        
        //设置whellview循环滚动
        day.setCyclic(true);
        minute.setCyclic(true);
        hour.setCyclic(true);
        second.setCyclic(true);
        
        //whellview设置监听
        year.addChangingListener(listener);
        month.addChangingListener(listener);
        day.addChangingListener(listener);
        hour.addChangingListener(listener);
        minute.addChangingListener(listener);
        second.addChangingListener(listener);
}


//设置Button的监听
private class MyPopuWindowsListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_showPopuWindow:
System.out.println("Click btn_ShowPopuWindow");
// 加上下面两行可以用back键关闭popupwindow,否则必须调用dismiss();
// 需要顺利让PopUpWindow dimiss;PopUpWindow的背景不能为空。
// 当有popuWindow.setFocusable(false);的时候,说明PopuWindow不能获得焦点,并不能点击外面消失,只能由dismiss()消失。
// 当设置为popuWindow.setFocusable(true);的时候,加上下面两行代码才会消失
ColorDrawable dw = new ColorDrawable(Color.HSVToColor(0x11,
new float[] { 00, 00, 0xff }));
popuWindow.setBackgroundDrawable(dw);
popuWindow.showAtLocation(findViewById(R.id.btn_showPopuWindow),
Gravity.CENTER, 0, 0);
popuWindow.setOutsideTouchable(true);
break;
case R.id.start:
popuWindow.dismiss();
break;
default:
break;
}
}


}


void updateDateTime(WheelView year, WheelView month,
WheelView day, WheelView hour, WheelView minute, WheelView second) {
String yeartext = String.valueOf(year.getCurrentItem()+2013);
String monthtext = String.valueOf(month.getCurrentItem()+1);
String daytext = String.valueOf(day.getCurrentItem()+1);
String hourtext = String.valueOf(hour.getCurrentItem());
String minutetext = String.valueOf(minute.getCurrentItem());
String secondtext = String.valueOf(second.getCurrentItem());
String total = yeartext+"-"+monthtext+"-"+daytext+" "+hourtext+":"+minutetext+":"+secondtext;
text.setText(total);

}
void updateDayOfMonth(WheelView day, int year,int month){
Calendar calendar = Calendar.getInstance();
int curDay = calendar.get(Calendar.DAY_OF_MONTH)-1;
if(((year%4 == 0)&&(year%400 != 0))||(year%400 == 0)){
if(month == 2){
       day.setViewAdapter(new DateNumericAdapter(this, 1, 29, curDay));
}else if(month == 4||month == 6||month == 9||month == 11){
day.setViewAdapter(new DateNumericAdapter(this, 1, 30, curDay));
}else{
day.setViewAdapter(new DateNumericAdapter(this, 1, 31, curDay));
}
}else{
if(month == 2){
       day.setViewAdapter(new DateNumericAdapter(this, 1, 28, curDay));
}else if(month == 4||month == 6||month == 9||month == 11){
day.setViewAdapter(new DateNumericAdapter(this, 1, 30, curDay));
}else{
day.setViewAdapter(new DateNumericAdapter(this, 1, 31, curDay));
}
}
}
private class DateArrayAdapter extends ArrayWheelAdapter {
        // Index of current item
        int currentItem;
        // Index of item to be highlighted
        int currentValue;
        
        /**
         * Constructor
         */
        public DateArrayAdapter(Context context, String[] items, int current) {
            super(context, items);
            this.currentValue = current;
            setTextSize(20);
        }
        
        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);
            if (currentItem == currentValue) {
                view.setTextColor(0xFF0000F0);
            }
            view.setTypeface(Typeface.SANS_SERIF);
        }
        
        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }
    
    private class DateNumericAdapter extends NumericWheelAdapter {
        // Index of current item
        int currentItem;
        // Index of item to be highlighted
        int currentValue;
        
        /**
         * Constructor
         */
        public DateNumericAdapter(Context context, int minValue, int maxValue, int current) {
            super(context, minValue, maxValue);
            this.currentValue = current;
            setTextSize(20);
        }
        
        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);
            if (currentItem == currentValue) {
                view.setTextColor(0xFF0000F0);
            }
            view.setTypeface(Typeface.SANS_SERIF);
        }
        
        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }
    
    private class TimeNumericAdapter extends NumericWheelAdapter {
        // Index of current item
        int currentItem;
        // Index of item to be highlighted
        int currentValue;
        
        // Values
        int minValue;
        
        
        String format;
        /**
         * Constructor
         */
        public TimeNumericAdapter(Context context, int minValue, int maxValue, int current,String format) {
            super(context, minValue, maxValue);
            this.currentValue = current;
            this.minValue = minValue;
            this.format = format;
            setTextSize(20);
        }
        
        @Override
        protected void configureTextView(TextView view) {
            super.configureTextView(view);
            if (currentItem == currentValue) {
                view.setTextColor(0xFF0000F0);
            }
            view.setTypeface(Typeface.SANS_SERIF);
        }
        
        @Override
public CharSequence getItemText(int index) {
        if (index >= 0 && index < getItemsCount()) {
                int value = minValue + index;
                return format != null ? String.format(format, value) : Integer.toString(value);
            }
            return null;
}


@Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            currentItem = index;
            return super.getItem(index, cachedView, parent);
        }
    }
    
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (popuWindow != null) {
if (popuWindow.isShowing()) {
popuWindow.dismiss();
}
}
}
}

注释都写得比较清楚,一会把源文件上传上去

源文件地址:http://download.csdn.net/detail/dahongwudi/6919497

你可能感兴趣的:(android)