Android中日期选择器与时间选择器的实现

在布局文件中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dip" >
    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日期"
        android:onClick="setDateClick"
        />
    <TextView
        android:id="@+id/tv1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     />
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="时间"
        android:onClick="setTimeClick"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     />

LinearLayout>

在MainActivity中的代码
@SuppressLint({ "NewApi", "ValidFragment" })
public class MainActivity extends Activity {
    private TextView tv_setDate,tv_setTime;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化对象,通过findViewById和相应的xml布局组件绑定
        tv_setDate=(TextView) findViewById(R.id.tv1);
        tv_setTime=(TextView) findViewById(R.id.tv2);
    }

    //设置日期事件处理方法
    public void setDateClick(View v){
        SetDateDialog sdt = new SetDateDialog();
        sdt.show(getFragmentManager(), "datePicker");
    }
    //设置时间事件处理方法
    public void setTimeClick(View v){
        SetTimeDialog std = new SetTimeDialog();
        std.show(getFragmentManager(), "timePicker");
    }
    //创建日期选择对话框
    class SetDateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{

        public Dialog onCreateDialog(Bundle savedInstanceState) {
            //Calendar 是一个抽象类,是通过getInstance()来获得实例,设置成系统默认时间
            final Calendar c = Calendar.getInstance();
            //获取年,月,日
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day= c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, year, month, day);

            return dpd;
        }
        public void onDateSet(DatePicker view, int year, int month, int day) {
            tv_setDate.setText(year+"年"+(month+1)+"月"+day+"日");
        }
    }
    @SuppressLint("NewApi")
    class SetTimeDialog extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

        @SuppressLint("NewApi")
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            //获取小时,分钟
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            TimePickerDialog tpd = new TimePickerDialog(getActivity(),this,hour, minute, DateFormat.is24HourFormat(getActivity()));
            return tpd;
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute){

            tv_setTime.setText(hourOfDay+":"+minute);
        }
    }
}
效果图:
 
  
 
  
 
  
 
 

你可能感兴趣的:(Android中日期选择器与时间选择器的实现)