DatePickerDialog的使用

来源:互联网 发布:龙舌兰推荐 知乎 编辑:程序博客网 时间:2024/04/30 09:14

首先需要在xml的布局中添加两个TextView用于显示日期。这里写图片描述

 <TextView        android:id="@+id/tv_date1"        android:layout_margin="30dp"        android:onClick="onClick"        android:textSize="30sp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="2016-08-24" />    <TextView        android:id="@+id/tv_date2"        android:layout_margin="30dp"        android:onClick="onClick"        android:textSize="30sp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="2016-08-24"  />

注意:android:onClick=”onClick”

在Activity中初始化TextView的值

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 private void initView() {        tv_date1 = (TextView) findViewById(R.id.tv_date1);        tv_date2 = (TextView) findViewById(R.id.tv_date2);        tv_date1.setText(sdf.format(new Date()));        tv_date2.setText(sdf.format(new Date()));    }

在Activity中添加OnClick方法用于文本的点击事件。

public void onClick(View view) {        switch (view.getId()) {            case R.id.tv_date1:                showDialog(tv_date1);                break;            case R.id.tv_date2:                showDialog(tv_date2);                break;        }    }

当用户点击文本的时候调用showDialog(TextView tv)方法,该方法用显示DatePickerDialog,并将选择的日期赋值到文本上。

下面就是showDialog的方法。
首先获取该文本中日期的年(year)月(month)日(day)。

String str = tv.getText().toString().trim();        try {            Date date = sdf.parse(str);            Calendar calendar = Calendar.getInstance();            calendar.setTime(date);            year = calendar.get(Calendar.YEAR);            month = calendar.get(Calendar.MONTH);            day = calendar.get(Calendar.DAY_OF_MONTH);        } catch (ParseException e) {            e.printStackTrace();        }

使该年月日初始化DatePickerDialog并显示:

        DatePickerDialog dialog = new DatePickerDialog(this,         new DatePickerDialog.OnDateSetListener() {            @Override            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {                //占位符可以使用0和#两种,当使用0的时候会严格按照样式来进行匹配,                //不够的时候会补0,而使用#时会将前后的0进行忽略                DecimalFormat df = new DecimalFormat("00");                String strMonth = df.format(monthOfYear + 1);                String strDay = df.format(dayOfMonth);                tv.setText(year + "-" + strMonth + "-" + strDay);            }        }, year, month, day);        dialog.show();

使用DecimalFormat将返回的月份和天数进行格式化
例如:
2016-8-12 ————>2016-08-12
2016-11-1 ————>2016-11-01

DecimalFormat中可以使用0和#两种占位符,当使用0的时候会严格按照样式来进行匹配,不够的时候会补0,而使用#时会将前后的0进行忽略。
如:

DecimalFormat df1 = new DecimalFormat("0.0"); DecimalFormat df2 = new DecimalFormat("#.#"); DecimalFormat df3 = new DecimalFormat("000.000"); DecimalFormat df4 = new DecimalFormat("###.###"); System.out.println(df1.format(12.34)); System.out.println(df2.format(12.34)); System.out.println(df3.format(12.34)); System.out.println(df4.format(12.34)); 

结果:

12.3

12.3

012.340

12.34

0 0
原创粉丝点击