您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
如何在Android开发中使用DatePicker与TimePicke监听时间?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
DatePicker和TimePicker都是从FrameLayout派生而来。
简单的例子实现对时间监听。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="选择时间" /> <DatePicker android:id="@+id/datepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <TimePicker android:id="@+id/timepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> </LinearLayout>
package com.ly; import java.util.Calendar; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.EditText; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; /** * @author kurting * 两个时间控件监听 * */ public class TimeExampleActivity extends Activity { private int year; private int month; private int day; private int hour; private int minute; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DatePicker datapicker = (DatePicker) findViewById(R.id.datepicker); TimePicker timepicker =(TimePicker) findViewById(R.id.timepicker); Calendar c =Calendar.getInstance(); year =c.get(Calendar.YEAR); month=c.get(Calendar.MONTH); day=c.get(Calendar.DAY_OF_MONTH); hour = c.get(Calendar.HOUR); minute =c.get(Calendar.MINUTE); datapicker.init(year, month, day, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { TimeExampleActivity.this.year=year; TimeExampleActivity.this.month=monthOfYear; TimeExampleActivity.this.day=dayOfMonth; showDate(year,month,day,hour,minute); } }); timepicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub TimeExampleActivity.this.hour=hourOfDay; TimeExampleActivity.this.minute=minute; } }); } private void showDate(int year, int month, int day, int hour,int minute) { EditText et = (EditText) findViewById(R.id.et); et.setText(year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"); } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。