当前位置:   article > 正文

DatePickerDialog---漂亮的弹出时间框_datepickerdialog美化

datepickerdialog美化


xml文件代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <EditText android:id="@+id/datePickerEdit" android:enabled="false"
  6. android:layout_width="200dip" android:layout_height="wrap_content" />
  7. <Button android:id="@+id/datePickerButton" android:text="请选择时间"
  8. android:layout_width="wrap_content" android:layout_height="wrap_content" />
  9. </LinearLayout>

activity代码:

  1. package cn.DatePickerDialog.demo;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import android.app.Activity;
  5. import android.app.DatePickerDialog;
  6. import android.app.DatePickerDialog.OnDateSetListener;
  7. import android.app.Dialog;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.DatePicker;
  13. import android.widget.EditText;
  14. public class DatePickerDialogDemoActivity extends Activity {
  15. private Button datePickerButton;
  16. private EditText datePickerEdit;
  17. private int m_year, m_month, m_day;
  18. private Calendar c;
  19. public void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. // 初始化控件
  23. datePickerEdit = (EditText) findViewById(R.id.datePickerEdit);
  24. datePickerButton = (Button) findViewById(R.id.datePickerButton);
  25. datePickerButton.setOnClickListener(new OnClickListener() {
  26. public void onClick(View arg0) {
  27. showDialog(0);
  28. }
  29. });
  30. c = Calendar.getInstance();// 获取日历的实例
  31. m_year = c.get(Calendar.YEAR);// 年
  32. m_month = c.get(Calendar.MONTH);// 月
  33. m_day = c.get(Calendar.DAY_OF_MONTH);// 日
  34. }
  35. protected Dialog onCreateDialog(int id) {
  36. if (id == 0) {
  37. return new DatePickerDialog(this, datePickerButtonListener, m_year,
  38. m_month, m_day);
  39. }
  40. return null;
  41. }
  42. private OnDateSetListener datePickerButtonListener = new OnDateSetListener() {
  43. public void onDateSet(DatePicker view, int year, int monthOfYear,
  44. int dayOfMonth) {
  45. // 将当前的设置的时间赋值到文本框中
  46. c.set(Calendar.YEAR, year);
  47. c.set(Calendar.MONTH, monthOfYear);
  48. c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
  49. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  50. datePickerEdit.setText(df.format(c.getTime()));
  51. }
  52. };
  53. }

项目目录结构:



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/278882
推荐阅读
相关标签
  

闽ICP备14008679号