[[185911]] Many applications have date pickers, such as setting the start and end time of some tasks. For the convenience of users and the good-looking interface, many applications use date pickers. I looked online. I personally feel that many date pickers are not very good-looking, but it is also a bit troublesome to modify them, so I wrote a demo. As for the interface effect, I personally feel that it is also very low-end. After all, I am not a UI expert, so I will just make do with it. These are not important because they can be modified. If you want to implement the year, month, and day, please see the content in the note below. The figure below is the implementation of the minute 00 15 30 45. If you want 0-59, please see the content in the note below. If you need an iOS-like app with the day of the week First, the interface pops up the PopupWindow animation. The specific code is as follows Enter the animation - <?xml version= "1.0" encoding= "utf-8" ?>
- < set xmlns:android= "http://schemas.android.com/apk/res/android" >
-
- <translate
- android:duration= "500"
- android:fromYDelta= "100.0%p"
- android:toYDelta= "45" />
-
- </ set >
Exit Animation - <?xml version= "1.0" encoding= "utf-8" ?>
- < set xmlns:android= "http://schemas.android.com/apk/res/android" >
-
- <translate
- android:duration= "500"
- android:fromYDelta= "0.0"
- android:toYDelta= "100.0%p" />
-
- </ set >
Contents of the main interface - public class MainActivity extends Activity implements View .OnClickListener{
- private TextView tv_house_time;
- private TextView tv_center;
- private WheelMain wheelMainDate;
- private String beginTime;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- initEvent();
- }
-
- private void initEvent() {
- tv_house_time.setOnClickListener(this);
- }
-
- private void initView() {
- tv_house_time = (TextView) findViewById(R.id.tv_house_time);
- tv_center = (TextView) findViewById(R.id.tv_center);
- }
-
- private java.text.DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
- public void showBottoPopupWindow() {
- WindowManager manager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
- Display defaultDisplay = manager.getDefaultDisplay();
- DisplayMetrics outMetrics = new DisplayMetrics();
- defaultDisplay.getMetrics(outMetrics);
- int width = outMetrics.widthPixels;
- View menuView = LayoutInflater. from (this).inflate(R.layout.show_popup_window, null );
- final PopupWindow mPopupWindow = new PopupWindow(menuView, ( int )(width*0.8),
- ActionBar.LayoutParams.WRAP_CONTENT);
- ScreenInfo screenInfoDate = new ScreenInfo(this);
- wheelMainDate = new WheelMain(menuView, true );
- wheelMainDate.screenheight = screenInfoDate.getHeight();
- String time = DateUtils.currentMonth().toString();
- Calendar calendar = Calendar.getInstance();
- if (JudgeDate.isDate( time , "yyyy-MM-DD" )) {
- try {
- calendar.setTime(new Date ( time ));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- int year = calendar.get ( Calendar.YEAR );
- int month = calendar.get ( Calendar.MONTH );
- int day = calendar.get(Calendar.DAY_OF_MONTH);
- int hours = calendar.get(Calendar.HOUR_OF_DAY);
- int minute = calendar.get(Calendar. MINUTE );
- wheelMainDate.initDateTimePicker( year , month , day , hours, minute );
- final String currentTime = wheelMainDate.getTime().toString();
- mPopupWindow.setAnimationStyle(R.style.AnimationPreview);
- mPopupWindow.setTouchable( true );
- mPopupWindow.setFocusable( true );
- mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
- mPopupWindow.showAtLocation(tv_center, Gravity.CENTER, 0, 0);
- mPopupWindow.setOnDismissListener(new poponDismissListener());
- backgroundAlpha(0.6f);
- 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( "Select start time" );
- tv_cancle.setOnClickListener(new View .OnClickListener() {
- @Override
- public void onClick( View arg0) {
- mPopupWindow.dismiss();
- backgroundAlpha(1f);
- }
- });
- tv_ensure.setOnClickListener(new View .OnClickListener() {
-
- @Override
- public void onClick( View arg0) {
- beginTime = wheelMainDate.getTime().toString();
- try {
- Date begin = dateFormat.parse(currentTime);
- Date end = dateFormat.parse(beginTime);
- tv_house_time.setText(DateUtils.currentTimeDeatil( begin ));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- mPopupWindow.dismiss();
- backgroundAlpha(1f);
- }
- });
- }
-
- public void backgroundAlpha( float bgAlpha) {
- WindowManager.LayoutParams lp = getWindow().getAttributes();
- lp.alpha = bgAlpha;
- getWindow().setAttributes(lp);
- }
-
- @Override
- public void onClick( View v) {
- switch (v.getId()){
- case R.id.tv_house_time:
- showBottoPopupWindow();
- break;
- }
- }
-
- class poponDismissListener implements PopupWindow.OnDismissListener {
- @Override
- public void onDismiss() {
- backgroundAlpha(1f);
- }
-
- }
- }
Layout content - <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
- android:id= "@+id/rel_select"
- android:layout_width= "wrap_content"
- android:layout_height= "wrap_content"
- android:layout_centerInParent= "true"
- android:layout_margin= "10dp"
- android:background= "@drawable/border_circle_radius"
- android:orientation= "vertical" >
- <TextView
- android:background= "#2F0F9980"
- android:padding= "10dp"
- android:id= "@+id/tv_pop_title"
- android:textSize= "18sp"
- android:gravity= "center"
- android:textColor= "#301616"
- android:layout_width= "match_parent"
- android:layout_height= "wrap_content" />
- <LinearLayout
- android:id= "@+id/timePicker1"
- android:layout_width= "match_parent"
- android:layout_height= "wrap_content"
- android:orientation= "horizontal" >
- <liuyongxiang.robert.com.testtime.wheelview.WheelView
- android:id= "@+id/year"
- android:layout_width= "wrap_content"
- android:layout_weight= "1"
- android:layout_height= "wrap_content" />
- <liuyongxiang.robert.com.testtime.wheelview.DashedLineView
- android:layout_width= "1dp"
- android:gravity= "center_vertical"
- android:layout_gravity= "center"
- android:background= "@drawable/dotted_line"
- android:layout_height= "match_parent" />
- <liuyongxiang.robert.com.testtime.wheelview.WheelView
- android:id= "@+id/month"
- android:layout_width= "wrap_content"
- android:layout_weight= "1"
- android:layout_height= "wrap_content" />
- <liuyongxiang.robert.com.testtime.wheelview.DashedLineView
- android:layout_width= "1dp"
- android:gravity= "center_vertical"
- android:layout_gravity= "center"
- android:background= "@drawable/dotted_line"
- android:layout_height= "match_parent" />
- <liuyongxiang.robert.com.testtime.wheelview.WheelView
- android:id= "@+id/day"
- android:layout_width= "wrap_content"
- android:layout_weight= "1"
- android:layout_height= "wrap_content" />
- <liuyongxiang.robert.com.testtime.wheelview.DashedLineView
- android:layout_width= "1dp"
- android:background= "@drawable/dotted_line"
- android:layout_height= "match_parent" />
- <liuyongxiang.robert.com.testtime.wheelview.WheelView
- android:id= "@+id/hour"
- android:layout_width= "wrap_content"
- android:layout_weight= "1"
- android:layout_height= "wrap_content" />
- <liuyongxiang.robert.com.testtime.wheelview.DashedLineView
- android:layout_width= "1dp"
- android:background= "@drawable/dotted_line"
- android:layout_height= "match_parent" />
- <liuyongxiang.robert.com.testtime.wheelview.WheelView
- android:id= "@+id/mins"
- android:layout_width= "wrap_content"
- android:layout_weight= "1"
- android:layout_height= "wrap_content" />
- </LinearLayout>
- <LinearLayout
- android:layout_width= "match_parent"
- android:layout_height= "wrap_content"
- android:background= "#2F0F9980"
- android:padding= "10dp"
- android:orientation= "horizontal" >
-
- <TextView
- android:id= "@+id/tv_cancle"
- android:layout_width= "0dp"
- android:layout_height= "wrap_content"
- android:gravity= "center"
- android:paddingLeft= "20dp"
- android:paddingRight= "20dp"
- android:padding= "5dp"
- android:textSize= "18sp"
- android:textColor= "#ff0000"
- android:layout_weight= "1"
- android:background= "@drawable/btn_pop"
- android:text= "Cancel" />
-
- <TextView
- android:layout_width= "0dp"
- android:layout_height= "wrap_content"
- android:layout_weight= "3" />
-
- <TextView
- android:id= "@+id/tv_ensure"
- android:layout_width= "0dp"
- android:layout_height= "wrap_content"
- android:gravity= "center"
- android:textColor= "#414341"
- android:padding= "5dp"
- android:paddingLeft= "20dp"
- android:paddingRight= "20dp"
- android:layout_weight= "1"
- android:textSize= "18sp"
- android:background= "@drawable/btn_pop"
- android:text= "OK" />
- </LinearLayout>
-
- </LinearLayout>
Please note The time display in MainActivity, tv_house_time.setText(DateUtils.currentTimeDeatil(begin)); needs to be changed to tv_house_time.setText(DateUtils.formateStringH(beginTime,DateUtils.yyyyMMddHHmm)); Otherwise the actual time is 00:00 Modified Change the following code in WheelMain - wv_mins.setAdapter(adapter);
- wv_mins.setCyclic( true ); // Cyclic scrolling
- wv_mins.setLabel("分"); // Add text
- int min = setMinute(m);
- wv_mins.setCurrentItem( min );
Replace with - wv_mins.setAdapter(new NumericWheelAdapter(
- 0, 59));
- wv_mins.setCyclic( true ); // Cyclic scrolling
- wv_mins.setLabel("分"); // Add text
- wv_mins.setCurrentItem(m);
It is also necessary to - int minute = Integer .valueOf(adapter.getItem(wv_mins.getCurrentItem()));
Change to - int minute = wv_mins.getCurrentItem();
Will change the minute from 0 to 59 If you don't want the time but only the year, month and day, you only need - 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);
-
- }
Just release this code and remove the code in the green area below. You also need to change the following code in MainActivty - wheelMainDate.initDateTimePicker( year , month , day , hours, minute );
Change to - wheelMainDate.initDateTimePicker( year , month , day );
And 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);
- }
Need to be modified to - 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);
- }
The effect is as follows |