android自定义日期和时间选择对话框得实现
2016-01-11 00:00
736 查看
1,先写布局文件,把时间选择器和日期选择器都放到一起去 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/write" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <DatePicker android:id="@+id/time_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" /> <TimePicker android:id="@+id/time_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" /> </LinearLayout> </LinearLayout>
2,写一个自定义控件实体类
/** * @author 作者 dpc * @date 创建时间:2015年12月26日 下11:41:50 * @parameter * @since * @return */ public class ShowTime { String data,time = "" ; public ShowTime(Context c,final ShowTimeIn s){ View view = LayoutInflater.from(c).inflate(R.layout.timescheh, null); final DatePicker da = (DatePicker) view.findViewById(R.id.time_data); Calendar calendar=Calendar.getInstance(); int year=calendar.get(Calendar.YEAR); int monthOfYear=calendar.get(Calendar.MONTH); int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH); data = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); da.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener(){ public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //dateEt.setText("您选择的日期是:"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日。"); data = "" ; data += year ; data += "-" ; if ((monthOfYear+1) < 10) { data += "0" ; data += (monthOfYear+1) ; }else { data += (monthOfYear+1) ; } data += "-" ; if (dayOfMonth < 10) { data += "0" ; data += dayOfMonth ; }else { data += dayOfMonth ; } } }); final TimePicker ti = (TimePicker) view.findViewById(R.id.time_time); int hour = calendar.get(Calendar.HOUR_OF_DAY); int m = calendar.get(Calendar.MINUTE); final int ss = calendar.get(Calendar.SECOND); ti.setCurrentHour(hour); //ti.setIs24HourView(true); if (hour < 10){ time += "0" ; time += hour ; }else { time += hour ; } time += ":" ; if (m < 10){ time += "0" ; time += m ; }else { time += m ; } time += ":" ; if (ss < 10){ time += "0" ; time += ss ; }else { time += ss ; } ti.setOnTimeChangedListener(new OnTimeChangedListener() { public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub time = "" ; if (hourOfDay < 10){ time += "0" ; time += hourOfDay ; }else { time += hourOfDay ; } time += ":" ; if (minute < 10){ time += "0" ; time += minute ; }else { time += minute ; } time += ":" ; if (ss < 10){ time += "0" ; time += ss ; }else { time += ss ; } } }); Dialog dialog = new AlertDialog.Builder(c) //.setIcon(android.R.drawable.ic_menu_week) //注入自己的布局文件 .setView(view) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub s.GetData(data, time); dialog.dismiss(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }) .create(); dialog.show(); } // 这里写了一个回调函数的接口 public interface ShowTimeIn { void GetData(String data ,String time); } }
3,此控件的用法获取选择的时间和日期。
// 设置时间日期 jckssj.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new ShowTime( mainactivity.this, new com.gaosheng.spyx.Fpmsmsmobile.one.ShowTime.ShowTimeIn() { @Override public void GetData(String data, String time) { // TODO Auto-generated method stub jckssj.setText(data + " " + time); sjStr = data + " " + time; } }); } });
相关文章推荐
- Android中Framgment的构造函数
- Android之AsyncTask两种线程池分析和总结
- Android APK反编译和代码混淆
- android view第一次执行动画无效
- android手机连接PC后,执行adb device报daemon not running. starting it now on port 5037 ADB server didn’t ACK错误
- Android实现自动定位城市并获取天气信息
- Android实现自动定位城市并获取天气信息
- Android布局自定义Shap圆形ImageView,可以单独设置背景与图片
- android135 360 来电去电归属地显示,自定义toast,
- android加密方式及原理
- Android之实现定位
- Android之实现定位
- android webview不能调用JS中的function?
- android 项目更改包名的方法
- ubuntu android 环境变量的配置
- android studio 使用xml:app命名空间代码不提示问题
- android opengl es 源码
- android studio链接不上真机解决方案
- 一个使用命令行编译Android项目的工具类
- android.content.ActivityNotFoundException: No Activity found to handle Intent