Android 滚动时间选择器

匿名 (未验证) 提交于 2019-12-03 00:43:02

显示效果一般般 , 做个参考吧

布局文件

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:padding="10dip" >      <TimePicker         android:timePickerMode="spinner"         android:id="@+id/time_picker"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="5dip"         android:layout_gravity="center" >     </TimePicker> </LinearLayout>

以上是 时间选择的布局文件,很简单

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:padding="10dip" >      <DatePicker         android:datePickerMode="spinner"         android:calendarViewShown="false"         android:id="@+id/date_picker"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="5dip"         android:layout_gravity="center" >     </DatePicker>  </LinearLayout>

以上是日期选择布局

 //点击"时间"按钮布局 设置时间         oplantime.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 //自定义控件                 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());                 View view = (LinearLayout) getLayoutInflater().inflate(R.layout.time_dialog, null);                 final TimePicker timePicker = (TimePicker) view.findViewById(R.id.time_picker);                 //初始化时间                 Calendar calendar = Calendar.getInstance();                 calendar.setTimeInMillis(System.currentTimeMillis());                 timePicker.setIs24HourView(true);                 timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));                 timePicker.setCurrentMinute(Calendar.MINUTE);                 //设置time布局                 builder.setView(view);                 builder.setTitle("设置时间信息");                 builder.setPositiveButton("确  定", new DialogInterface.OnClickListener() {                     @Override                     public void onClick(DialogInterface dialog, int which) {                         mHour = timePicker.getCurrentHour();                         mMinute = timePicker.getCurrentMinute();                         //时间小于10的数字 前面补0 如01:12:00                         oplantime.setText(new StringBuilder().append(mHour < 10 ? "0" + mHour : mHour).append(":")                                 .append(mMinute < 10 ? "0" + mMinute : mMinute).append(":00") );                         dialog.cancel();                     }                 });                 builder.setNegativeButton("取  消", new DialogInterface.OnClickListener() {                     @Override                     public void onClick(DialogInterface dialog, int which) {                         dialog.cancel();                     }                 });                 builder.create().show();             }         });     }      public void setDate(){         //点击"日期"按钮布局 设置日期         oplandate.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 //通过自定义控件AlertDialog实现                 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());                 View view = (LinearLayout) getLayoutInflater().inflate(R.layout.date_dialog, null);                 final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);                 //设置日期简略显示 否则详细显示 包括:星期\周                 datePicker.setCalendarViewShown(false);                 //初始化当前日期                 Calendar calendar = Calendar.getInstance();                 calendar.setTimeInMillis(System.currentTimeMillis());                 datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),                         calendar.get(Calendar.DAY_OF_MONTH), null);                 //设置date布局                 builder.setView(view);                 builder.setTitle("设置日期信息");                 builder.setPositiveButton("确  定", new DialogInterface.OnClickListener() {                     @Override                     public void onClick(DialogInterface dialog, int which) {                         //日期格式                         StringBuffer sb = new StringBuffer();                         sb.append(String.format("%d-%02d-%02d",                                 datePicker.getYear(),                                 datePicker.getMonth() + 1,                                 datePicker.getDayOfMonth()));                         oplandate.setText(sb);                         //赋值后面闹钟使用                         mYear = datePicker.getYear();                         mMonth = datePicker.getMonth();                         mDay = datePicker.getDayOfMonth();                         dialog.cancel();                     }                 });                 builder.setNegativeButton("取  消", new DialogInterface.OnClickListener() {                     @Override                     public void onClick(DialogInterface dialog, int which) {                         dialog.cancel();                     }                 });                 builder.create().show();             }         });     }

代码很简单 ,只需要在 Activity中 给 日期 和时间 按钮

dateBtn.setOnclickListener( setDate());

timeBtn.setOnclickListener( setTime());

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!