时间控件

    xiaoxiao2021-04-19  113

    需要导入下面的依赖:

    compile 'com.contrarywind:Android-PickerView:3.2.1' selectedDate = Calendar.getInstance(); time1Layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { initBeginTime(time1Tv.getText().toString()); timePickerView = new TimePickerView.Builder(VisorRegistActivity.this, new TimePickerView.OnTimeSelectListener() { @Override public void onTimeSelect(Date date, View v) { time1Tv.setText(DateTimeDialog.getTime(date)); } }).setDate(selectedDate).build(); timePickerView.show(); } }); //通过手动截取来设置开始时间 private void initBeginTime(String time) { if (selectedDate != null) selectedDate.set(Integer.valueOf(time.substring(0, 4)), Integer.valueOf(time.substring(5, 7)) - 1 , Integer.valueOf(time.substring(8, 10)), Integer.valueOf(time.substring(11, 13)) , Integer.valueOf(time.substring(14, 16)), Integer.valueOf(time.substring(17, 19))); } public class DateTimeDialog { public static void showDateTime(Activity activity, final TextView textView) { AlertDialog.Builder builder = new AlertDialog.Builder(activity); View view = View.inflate(activity, R.layout.date_time_dialog, null); final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker); final TimePicker timePicker = (TimePicker) view.findViewById(R.id.time_picker); builder.setView(view); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), null); timePicker.setIs24HourView(true); timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY)); timePicker.setCurrentMinute(cal.get(Calendar.MINUTE)); int inType = textView.getInputType(); textView.setInputType(InputType.TYPE_NULL); textView.setInputType(inType); builder.setTitle("日期选择:"); builder.setPositiveButton("确 定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); sb.append(String.format("%d-d-d", datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth())); sb.append(" "); sb.append(timePicker.getCurrentHour()).append(":").append(timePicker.getCurrentMinute()).append(":00"); textView.setText(sb); dialog.cancel(); } }); Dialog dialog = builder.create(); dialog.show(); } public static String getTime(Date date) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(date); } } DateTimeDialog 的XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="10dp" android:gravity="center" > <DatePicker android:id="@+id/date_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:calendarViewShown="false" /> <TimePicker android:id="@+id/time_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:layout_gravity="center_vertical"/> </LinearLayout>

    转载请注明原文地址: https://ju.6miu.com/read-675920.html

    最新回复(0)