DatePicker+TimePicker 我会带着你远行 2024-02-18 18:56 37阅读 0赞 ### **1 DatePicker** ### ![Center][] ![Center 1][] ![Center 2][] **Main.xml** <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <TextView android:id="@+id/dateDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="20sp" android:text="" /> <Button android:id="@+id/pickDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="修改时间" /> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> **MainActivity.java** public class MainActivity extends Activity { TextView mDateDisplay; Button mPickDate; DatePicker datePicker; private int mYear, mMonth, mDay; private static final int DATE_DIALOG_ID = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDateDisplay = (TextView) findViewById(R.id.dateDisplay); mPickDate = (Button) findViewById(R.id.pickDate); mPickDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); updateDisplay(); datePicker = (DatePicker) findViewById(R.id.datePicker); datePicker.init(mYear, mMonth, mDay, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this, year+"-"+(monthOfYear+1), Toast.LENGTH_LONG).show(); } }); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); default: break; } return null; } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateDisplay(); } }; private void updateDisplay() { mDateDisplay.setText(new StringBuilder().append("时间:") .append(mYear).append("-") .append(mMonth+1).append("-") .append(mDay)); } } ### 2 TimePicker ### **![Center 3][] ![Center 4][]** **MainActivity.java** public class MainActivity extends Activity { TextView mTimeDisplay; Button mPickTime; TimePicker TimePicker; private int mHour, mMinute; private static final int Time_DIALOG_ID = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTimeDisplay = (TextView) findViewById(R.id.dateDisplay); mPickTime = (Button) findViewById(R.id.pickDate); mPickTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(Time_DIALOG_ID); } }); final Calendar c = Calendar.getInstance(); // for (String str : TimeZone.getAvailableIDs()) { // System.out.println(str); // } c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); upTimeDisplay(); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case Time_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true); default: break; } return null; } private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; upTimeDisplay(); } }; private void upTimeDisplay() { mTimeDisplay.setText(new StringBuilder().append("时间:") .append(pad(mHour)).append(":") .append(pad(mMinute))); } private static String pad(int c) { if(c>=10) { return String.valueOf(c); } else { return "0"+String.valueOf(c); } } } [Center]: https://img-blog.csdn.net/20170601102908945?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzk0MzQyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [Center 1]: https://img-blog.csdn.net/20170601102944711?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzk0MzQyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [Center 2]: https://image.dandelioncloud.cn/pgy_files/images/2024/01/29/5062437d38b0436cb7719d3880c203d7.png [Center 3]: https://img-blog.csdn.net/20170601105406692?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzk0MzQyMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [Center 4]: https://image.dandelioncloud.cn/pgy_files/images/2024/01/29/c1ab676798414616a21cfe8bc73e3058.png
还没有评论,来说两句吧...