标签:修改时间对话框
以对话框的形式弹出日期设置:
首先在xml中创建一个EditText输入框:
<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));
}
本文出自 “Android小笔记” 博客,请务必保留此出处http://dreamwing.blog.51cto.com/9872128/1611691
标签:修改时间对话框
原文地址:http://dreamwing.blog.51cto.com/9872128/1611691