Android中的日期选择器 [英] Date picker in Android

查看:128
本文介绍了Android中的日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在Android中为简单的日期选择器发布示例代码。

Can any one post sample code for a simple date picker in Android.

如果Android中无法选择日期选择器,则需要选择日期的选项。

If date picker is not possible in Android, an option to choose a date is needed.

推荐答案

使用DatePicker

Use the DatePicker

http://developer.android.com/reference/android/widget/DatePicker.html

从API级别1起可以使用

It is availible since API Level 1

这里有一个使用DatePickerDialog的例子。

Here a example how to use the DatePickerDialog.

首先在你的layout.xml中添加一个TextView和一个Button

First add a TextView and a Button to your layout.xml

<Button android:id="@+id/myDatePickerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose Date"/>

<TextView android:id="@+id/showMyDate"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/>

接下来,您必须在布局的onCreate方法中初始化Button和TextView。
你需要这个类变量

Next you have to initialize the Button and TextView in the onCreate Method of your layout. You need this class variables

private int mYear;
private int mMonth;
private int mDay;

private TextView mDateDisplay;
private Button mPickDate;

static final int DATE_DIALOG_ID = 0;

这里是onCreate方法

Here the onCreate method

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mDateDisplay = (TextView) findViewById(R.id.showMyDate);        
    mPickDate = (Button) findViewById(R.id.myDatePickerButton);

    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    // display the current date
    updateDisplay();
}

UpdateDisplay方法:

UpdateDisplay method:

private void updateDisplay() {
    this.mDateDisplay.setText(
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" "));
}

DatePickDialog的回调侦听器

The callback listener for the DatePickDialog

private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDisplay();
        }
    };

由showDialog()调用的onCreateDialog方法$ /

The onCreateDialog method, called by showDialog()

@Override
protected Dialog onCreateDialog(int id) {
   switch (id) {
   case DATE_DIALOG_ID:
      return new DatePickerDialog(this,
                mDateSetListener,
                mYear, mMonth, mDay);
   }
   return null;
}

希望它有帮助,使用它,它的工作正常。

Hope it helps, used it and it works fine.

http://developer.android.com/guide/tutorials/views/hello-datepicker.html

这篇关于Android中的日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆