标签:
1.layout
<RelativeLayout 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" > <Button android:id="@+id/button01_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showTimePickerDialog" android:text="@string/pick_time" /> <Button android:id="@+id/btn_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button01_time" android:onClick="showDatePickerDialog" android:text="@string/pick_date" /> <TextView android:id="@+id/tv_settime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button01_time" android:layout_alignBottom="@+id/button01_time" android:layout_toRightOf="@+id/button01_time" /> <TextView android:id="@+id/tv_setdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btn_date" android:layout_alignBottom="@+id/btn_date" android:layout_toRightOf="@+id/btn_date" /> </RelativeLayout>
2.string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Android_020</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="pick_time">时间设置</string> <string name="pick_date">日期设置</string> <string name="settime">时间==设置</string> <string name="setdate">日期==设置</string> </resources>
3.ActivityMain.java
package com.xrhou.android_020; import java.util.Calendar; import android.annotation.SuppressLint; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.Dialog; import android.app.DialogFragment; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.text.format.DateFormat; import android.view.Menu; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import android.widget.TimePicker; /** * 时间日期选择对话框 * * @author hbj403 */ @SuppressLint("ValidFragment") public class MainActivity extends Activity { private TextView tv_settime,tv_setdate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_settime=(TextView) findViewById(R.id.tv_settime); tv_setdate=(TextView) findViewById(R.id.tv_setdate); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } //时间选择器 public void showTimePickerDialog(View view){ TimeDialogMe tpf=new TimeDialogMe(); tpf.show(getFragmentManager(), "showMyTime"); } //日期选择器 public void showDatePickerDialog(View view){ DateDialogMe ddm=new DateDialogMe(); ddm.show(getFragmentManager(), "showMyDatet"); } //时间选择器 class TimeDialogMe extends DialogFragment implements OnTimeSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar calendar=Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); TimePickerDialog tpd=new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); return tpd; } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { tv_settime.setText(hourOfDay+":"+minute); } } //日期选择器 class DateDialogMe extends DialogFragment implements OnDateSetListener{ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c=Calendar.getInstance(); int year=c.get(Calendar.YEAR); int month=c.get(Calendar.MONTH); //Calendar的month对应的是0-11 int day=c.get(Calendar.DAY_OF_MONTH); DatePickerDialog dpd=new DatePickerDialog(getActivity(), this, year, month, day); return dpd; } @Override public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) { tv_setdate.setText(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日"); } } }
标签:
原文地址:http://www.cnblogs.com/xrhou12326/p/4463459.html