欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android之datepicker控件的用法

程序员文章站 2024-03-05 20:50:37
如下所示:

如下所示:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  >
<textview 
  android:id="@+id/textviewdatedisplay" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  />
<button 
  android:id="@+id/btnpickdate"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="change the date"
  />
</linearlayout>
package com.example.mars_2200_datepicker;

import java.util.calendar;

import android.support.v7.app.actionbaractivity;
import android.support.v7.app.actionbar;
import android.support.v4.app.fragment;
import android.app.datepickerdialog;
import android.app.dialog;
import android.os.bundle;
import android.view.layoutinflater;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.viewgroup;
import android.widget.button;
import android.widget.datepicker;
import android.widget.textview;
import android.os.build;

public class mainactivity extends actionbaractivity {
  private textview textviewdatedisplay;
  private button btnpickdate;
  private int myear;
  private int mmonth;
  private int mday;

  static final int date_dialog_id = 0;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    textviewdatedisplay = (textview) findviewbyid(r.id.textviewdatedisplay);
    btnpickdate = (button) findviewbyid(r.id.btnpickdate);

    btnpickdate.setonclicklistener(new view.onclicklistener() {
      public void onclick(view v) {
        showdialog(date_dialog_id);
      }
    });
    final calendar c = calendar.getinstance();
    myear = c.get(calendar.year);
    mmonth = c.get(calendar.month);
    mday = c.get(calendar.day_of_month);

    updatedisplay();
  }

  @override
  public boolean oncreateoptionsmenu(menu menu) {

    // inflate the menu; this adds items to the action bar if it is present.
    getmenuinflater().inflate(r.menu.main, menu);
    return true;
  }

  @override
  public boolean onoptionsitemselected(menuitem item) {
    // handle action bar item clicks here. the action bar will
    // automatically handle clicks on the home/up button, so long
    // as you specify a parent activity in androidmanifest.xml.
    int id = item.getitemid();
    if (id == r.id.action_settings) {
      return true;
    }
    return super.onoptionsitemselected(item);
  }

  /**
   * a placeholder fragment containing a simple view.
   */
  public static class placeholderfragment extends fragment {

    public placeholderfragment() {
    }

    @override
    public view oncreateview(layoutinflater inflater, viewgroup container,
        bundle savedinstancestate) {
      view rootview = inflater.inflate(r.layout.fragment_main, container,
          false);
      return rootview;
    }
  }

  private void updatedisplay() {
    textviewdatedisplay.settext(new stringbuilder().append(myear).append("-")
        .append(mmonth + 1).append("-").append(mday).append(" "));
  }

  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();
    }
  };

  protected dialog oncreatedialog(int id) {
    switch (id) {
    case date_dialog_id:
      return new datepickerdialog(this, mdatesetlistener, myear, mmonth,
          mday);
    }
    return null;
  }
}

以上就是小编为大家带来的android之datepicker控件的用法的全部内容了,希望对大家有所帮助,多多支持~