码迷,mamicode.com
首页 > 移动开发 > 详细

Android小笔记之对话框形式修改日期

时间:2015-02-05 10:49:03      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

  以对话框的形式弹出日期设置:

  首先在xml中创建一个EditText输入框:

  android:id="@+id/et_time"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  //设置输入框点击事件

  android:onClick="dateshow" />

  在主方法的onCreate中设置修改时间

  private EditText time;

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  time = (EditText) findViewById(R.id.et_time);

  Calendar calendar = Calendar.getInstance();

  //获取日期

  newyear = calendar.get(Calendar.YEAR);

  //因为月份从0--11所以要加1

  newmonth = calendar.get(Calendar.MONTH) + 1;

  newday = calendar.get(Calendar.DAY_OF_MONTH);

  update();

  }

  public void dateshow(View v) {

  //点击EditText时以对话框形式弹出修改日期

  onCreatDialog(DATE_SHOW).show();

  }

  protected Dialog onCreatDialog(int id) {

  switch (id) {

  case DATE_SHOW:

  return new DatePickerDialog(incomeActivity.this, dateSetListener,

  newyear, newmonth, newday);

  }

  return null;

  }

  private DatePickerDialog.OnDateSetListener dateSetListener = new OnDateSetListener() {

  @Override

  public void onDateSet(DatePicker view, int year, int monthOfYear,

  int dayOfMonth) {

  // TODO Auto-generated method stub

  //修改年份

  newyear = year;

  //修改月份

  newmonth = monthOfYear;

  //修改日期

  newday = dayOfMonth;

  update();

  }

  };

  private void update() {

  // TODO Auto-generated method stub

  //用于多个字符串的拼接

  StringBuffer sb = new StringBuffer();

  //设置修改后的日期时间到EditText中

  time.setText(sb.append(newyear).append("-").append(newmonth)

  .append("-").append(newday));

  }

  资料选摘:

http://zhidao.baidu.com/question/200091994091382685.html
http://zhidao.baidu.com/question/489453852244345172.html
http://zhidao.baidu.com/question/1755320451253035788.html
http://zhidao.baidu.com/question/1755320514741383788.html
http://zhidao.baidu.com/question/304952540611099404.html
http://zhidao.baidu.com/question/489453916520558332.html
http://zhidao.baidu.com/question/1669192806399603427.html
http://zhidao.baidu.com/question/1047411913542707699.html
http://zhidao.baidu.com/question/264028518446024725.html
http://zhidao.baidu.com/question/1669192870606144187.html
http://zhidao.baidu.com/question/1669256295134138147.html
http://zhidao.baidu.com/question/489517469334454852.html
http://zhidao.baidu.com/question/264028518512175285.html
http://zhidao.baidu.com/question/489517725576610092.html
http://zhidao.baidu.com/question/1047475658783201859.html
http://zhidao.baidu.com/question/1047475850758399659.html
http://zhidao.baidu.com/question/1047475722991462899.html
http://zhidao.baidu.com/question/489517853993746332.html

Android小笔记之对话框形式修改日期

标签:

原文地址:http://www.cnblogs.com/fengmandaren/p/4273969.html

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