默认显示当前时间:
time.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="日期和时间控件的使用DEMO" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/showdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/pickdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择日期" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/showtime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/picktime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择时间" />
</LinearLayout>
</LinearLayout>
TimeActivity.java
public class TimeActivity extends Activity {
private EditText showDate = null;
private Button pickDate = null;
private EditText showTime = null;
private Button pickTime = null;
private static final int SHOW_DATAPICK = 0;
private static final int DATE_DIALOG_ID = 1;
private static final int SHOW_TIMEPICK = 2;
private static final int TIME_DIALOG_ID = 3;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
initializeViews();
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
setDateTime();
setTimeOfDay();
}
/**
* 初始化控件和UI视图
*/
private void initializeViews(){
showDate = (EditText) findViewById(R.id.showdate);
pickDate = (Button) findViewById(R.id.pickdate);
showTime = (EditText)findViewById(R.id.showtime);
pickTime = (Button)findViewById(R.id.picktime);
pickDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg = new Message();
if (pickDate.equals((Button) v)) {
msg.what = TimeActivity.SHOW_DATAPICK;
}
TimeActivity.this.dateandtimeHandler.sendMessage(msg);
}
});
pickTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg = new Message();
if (pickTime.equals((Button) v)) {
msg.what = TimeActivity.SHOW_TIMEPICK;
}
TimeActivity.this.dateandtimeHandler.sendMessage(msg);
}
});
}
/**
* 设置日期
*/
private void setDateTime(){
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDateDisplay();
}
/**
* 更新日期显示
*/
private void updateDateDisplay(){
showDate.setText(new StringBuilder().append(mYear).append("-")
.append((mMonth + 1) < 10 ? "0" + (mMonth + 1) : (mMonth + 1)).append("-")
.append((mDay < 10) ? "0" + mDay : mDay));
}
/**
* 日期控件的事件
*/
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDateDisplay();
}
};
/**
* 设置时间
*/
private void setTimeOfDay(){
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
updateTimeDisplay();
}
/**
* 更新时间显示
*/
private void updateTimeDisplay(){
showTime.setText(new StringBuilder().append(mHour).append(":")
.append((mMinute < 10) ? "0" + mMinute : mMinute));
}
/**
* 时间控件事件
*/
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateTimeDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
case TIME_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break;
}
}
/**
* 处理日期和时间控件的Handler
*/
Handler dateandtimeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case TimeActivity.SHOW_DATAPICK:
showDialog(DATE_DIALOG_ID);
break;
case TimeActivity.SHOW_TIMEPICK:
showDialog(TIME_DIALOG_ID);
break;
}
}
};
}
分享到:
相关推荐
Android 自定义控件简单Demo 图片+文字
Android日期和时间的设置DEMO 控件
包括文本,按钮,图片按钮,复选框,单选框,钟表,电子表,图片显示,日期选择控件,时间选择控件进度条控件,视频控件,浏览器控件,评分控件,选项卡控件,下拉控件,计时器控件,缩略图控件,图片转换器控件,...
本博客 http://blog.csdn.net/u012792686 自定义控件源码
安卓Android源码——自定义控件入门级demo.zip
android 所有控件 包括AnimationDemo、 AutoCompleteDemo、ChronometerDemo、DatePickerDemo、ExpandableListDemo、GalleryDemo、GridDemo、ImageSwitcherDemo、ProgressBarDemo、RatingBarDemo、SeekBarDemo、...
一个Demo搞定30个控件 基于Eclipse的android工程源码demo
android控件demo
自定一个控件button 这个控件由一个ImageView和一个TextView两个控件组成,控件有加标签
android 常用控件实例23个demo,把这个例子看懂了,书基本不用看了,平时用的控件都在里面,非常适合新手学习。
android 基本控件demo大集合(android studio版本),为初学者带了很大的方便
这个Demo主要实现如何将Android下几个原生的控件封装到一起,产生一个新的控件,称之为“android自定义组合控件”,这种控件可以简化代码,大家有空下载看看吧,谢谢!
Android仿Iphone的日期控件的Demo,Android滚动日期控件,Android日期控件,可以自己定义一个自己想要的日期控件。非常不错的Demo。
Android通过webview控件实现webapp的demo,使用的是 androidstudio,可以实现跟html页面的js交互等
Android SlidingMenu控件的使用demo
android自定义控件学习demo,自定义继承控件,自定义自绘View控件,自定义组合控件
此为示例代码,详细讲解请参考:http://blog.csdn.net/u012604745/article/details/73277245 滚动字幕:(可设置属性:点击暂停,水平垂直,速度,次数,文字内容,文字颜色,文字大小)
1.在画布中的控件A长按能进行控件的连接,只有在控件B范围内抬起控件之间的连线才能连接成功; 2.当控件连线成功后,拖动控件AB之间的连线随着控件的拖动随之变化; 3.控件连线在屏幕上随着手势滑动触碰到连接则...
一个可以掌握所有android控件的demo,欢迎广大朋友下载使用!我从中受益不浅,所以特拿来与天底下对正在学习android的朋友们一起分享,里面有一个压缩包,解压后有五个工程文件,通过Import导入到eclipse里去,然后...
Android自定义控件的使用与自定义控件中使用自定义attrs.xml属性标签