Android控件之TimePicker和DatePicker

本文介绍如何使用Android的DatePicker和TimePicker控件实现日期和时间的选择功能。通过示例代码展示了如何创建日期和时间选择对话框,并获取所选的日期和时间。

Android控件之TimePicker和DatePicker

1 Picker简介

DatePickerTimePicker分别提供日期和时间的选择试图;通过它们得到的日期和时间是格式化的。

2 Picker示例

写一个activity,包含一个“日期”按钮和一个“时间”按钮。
点击“日期”按钮,进入“日期”选择界面。
点击“时间”按钮,进入“时间”选择界面。

应用层代码

package com.skywang.picker;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.support.v4.app.DialogFragment;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.app.DatePickerDialog;
import     android.text.format.DateFormat;
import android.widget.TimePicker;
import android.widget.DatePicker;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import java.util.Calendar;

public class PickerTest extends FragmentActivity {
    private static final String TAG = "SKYWANG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picker_test);
    }
    
    // "时间"按钮onClick
    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }
    
    public static class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);
            
            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }
        
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            Log.d(TAG, "hour:"+hourOfDay+", minute: "+minute);
        }
    }
    
    // “日期”按钮onClick
    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }
    
    public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            
            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }
        
        public void onDateSet(DatePicker view, int year, int month, int day) {
            Log.d(TAG, "year:"+year+", month: "+month+", day:"+day);
        }
    }
        
}

layout文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="@string/pick_time" 
    android:onClick="showTimePickerDialog" />

    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="@string/pick_date" 
    android:onClick="showDatePickerDialog" />
    
</LinearLayout>

manifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.skywang.picker"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.skywang.picker.PickerTest"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

点击下载:源代码

运行效果:如图

 

日期选择窗口

 

时间选择窗口

内容概要:本文围绕“基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究”展开,提出了一种结合Koopman算子理论与递归神经网络(RNN)的数据驱动建模方法,旨在对非线性纳米定位系统进行有效线性化建模,并实现高精度的模型预测控制(MPC)。该方法利用Koopman算子将非线性系统映射到高维线性空间,通过递归神经网络学习系统的动态演化规律,构建可解释性强、计算效率高的线性化模型,进而提升预测控制在复杂不确定性环境下的鲁棒性与跟踪精度。文中给出了完整的Matlab代码实现,涵盖数据预处理、网络训练、模型验证与MPC控制器设计等环节,具有较强的基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)可复现性工程应用价值。; 适合人群:具备一定控制理论基础Matlab编程能力的研究生、科研人员及自动化、精密仪器、机器人等方向的工程技术人员。; 使用场景及目标:①解决高精度纳米定位系统中非线性动态响应带来的控制难题;②实现复杂机电系统的数据驱动建模与预测控制一体化设计;③为非线性系统控制提供一种可替代传统机理建模的有效工具。; 阅读建议:建议结合提供的Matlab代码逐模块分析实现流程,重点关注Koopman观测矩阵构造、RNN网络结构设计与MPC控制器耦合机制,同时可通过替换实际系统数据进行迁移验证,深化对数据驱动控制方法的理解与应用能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值