码迷,mamicode.com
首页 > 其他好文 > 详细

DatePickDialog与TimePickerDialog

时间:2015-08-20 20:52:07      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:dialog   datepickdialog   

写一个DateDialog的Demo,
步骤:
1、定义两个按钮,一个控制打开日期Dialog,一个控制打开时间Dialog。
2、通过new关键字创建DatePickerDialog,调用它们的show()方法即可显示。
3、为DataPickerDialog、TimePickerDialog绑定监听器,保证用户通过DatePickerDialog、

TimePickerDialog设置时间触发监听器。通过监听器获取用户设置的事件。

xml文件

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button
            android:id="@+id/dateDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择时间"/>
        <Button
            android:id="@+id/timeDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择时间"/>
        <EditText
            android:id="@+id/dateText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <EditText
            android:id="@+id/timeText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


</RelativeLayout>
MainActivity.java

package lzl.edu.com.datedialog;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import java.util.Calendar;


public class MainActivity extends Activity implements View.OnClickListener {
    Button dateDialog,timeDialog;
    EditText dateText,timeText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dateDialog = (Button) findViewById(R.id.dateDialog);
        timeDialog = (Button) findViewById(R.id.timeDialog);
        dateText = (EditText) findViewById(R.id.dateText);
        timeText = (EditText) findViewById(R.id.timeText);

        dateDialog.setOnClickListener(this);
        timeDialog.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Calendar calendar = Calendar.getInstance();
        switch (v.getId()) {
            case R.id.dateDialog:
                new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                String info = "您选择的日期是:";
                                info += year + "年" + monthOfYear + "月" + dayOfMonth + "日";
                                dateText.setText(info);
                            }
                        }, calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
                break;
            case R.id.timeDialog:
                new TimePickerDialog(MainActivity.this,
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                String timeInfo = "您选择的时间是:";
                                timeInfo += hourOfDay+"时"+minute+"分";
                                timeText.setText(timeInfo);
                            }
                        },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();
                break;
            default: break;
        }
    }
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

DatePickDialog与TimePickerDialog

标签:dialog   datepickdialog   

原文地址:http://blog.csdn.net/u011521890/article/details/47814061

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!