android日期筛选_Android开发笔记:日期/时间选择器

本文介绍了Android中日期和时间选择器的两种实现方式:显式和隐式。显式方式通过DatePicker和TimePicker控件展示,隐式则借助Calendar创建对话框。详细步骤包括布局设置、控件绑定、事件监听及接口实现。
摘要由CSDN通过智能技术生成

本章记录日期与时间选择器各自对应的两种实现方式:隐式/显式,隐式为未点击时隐藏,显式为始终占用。

一、日期选择器

1.1 显式:使用DatePicker

①布局页面为DatePicker+Button

android:id="@+id/dp_dickdate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:id="@+id/btn_yesdate"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="确定"

android:gravity="center"

android:textSize="20sp"/>

②控件的绑定

dp_dickdate = (DatePicker)findViewById(R.id.dp_dickdate);

findViewById(R.id.btn_yesdate).setOnClickListener(this);

③点击后直接使用其对应的方法getYear()、getMonth()、getDayOfMonth()即可

String desc = String.format("选中日期%d年%d月%d日",dp_dickdate.getYear(),dp_dickdate.getMonth()+1,dp_dickdate.getDayOfMonth());

tv_disdate.setText(desc);

④实现效果

1.2 隐式:使用Calendar

①布局页面只用一个按钮

android:id="@+id/btn_chooseDate"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="选择日期"

android:gravity="center"

android:textSize="20sp"/>

②页面代码需要实现接口implementsDatePickerDialog.OnDateSetListener,并实现函数onDateSet

public void onDateSet(DatePicker dp,int year,int month,int day){

String desc = String.format("选中日期%d年%d月%d日",year,month+1,day);

tv_disdate.setText(desc);

}

③在点击按钮后响应如下:

if(view.getId()==R.id.btn_chooseDate){

//获取实例,包含当前年月日

Calendar calendar = Calendar.getInstance();

DatePickerDialog dialog = new DatePickerDialog(this,this,

calendar.get(Calendar.YEAR),

calendar.get(Calendar.MARCH),

calendar.get(Calendar.DAY_OF_MONTH));

dialog.show();

}

④实现效果

二、时间选择器

2.1 显式:使用TimePicker

①布局页面为TimePicker+Button

android:id="@+id/tp_time"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:id="@+id/btn_yestime"

android:layout_width="match_parent"

android:layout_height="50dp"

android:textSize="20sp"

android:text="确定"

android:gravity="center"/>

②控件绑定

btn_yestime = (Button)findViewById(R.id.btn_yestime);

tp_time = (TimePicker)findViewById(R.id.tp_time);

btn_yestime.setOnClickListener(this);

③确认选择后显示时间

if(view.getId()==R.id.btn_yestime){

String desc = String.format("选择时间%d时%d分",tp_time.getCurrentHour(),tp_time.getCurrentMinute());

tv_distime.setText(desc);

}

④实现效果

2.2 隐式:使用Calendar

①页面布局也只用一个按钮

android:id="@+id/btn_choosetime"

android:layout_width="match_parent"

android:layout_height="50dp"

android:gravity="center"

android:text="选择时间"

android:textSize="20sp"/>

②同样,要实现接口TimePickerDialog.OnTimeSetListener的onTimeSet函数

public void onTimeSet(TimePicker tp,int hour,int minute){

String desc = String.format("您选择%d时%d分",hour,minute);

tv_distime.setText(desc);

}

③点击按钮后响应事件

if(view.getId()==R.id.btn_choosetime){

Calendar calendar = Calendar.getInstance();

TimePickerDialog dialog = new TimePickerDialog(this,this,

calendar.get(Calendar.HOUR_OF_DAY),

calendar.get(Calendar.MINUTE),

true);

dialog.show();

}

④实现效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Android Studio失物招领模块Xml文件的实现代码,请修改代码实现下拉以及图片上传功 <TextView android:id="@+id/shiwuzhaolingzi" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择是失物还是招领" /> <Spinner android:id="@+id/spinner_lost_found" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/lost_found_array" /> <TextView android:id="@+id/text_item_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="物品名称" /> <EditText android:id="@+id/edit_item_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="物品描述" /> <EditText android:id="@+id/edit_item_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="时间" /> <EditText android:id="@+id/edit_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="地点" /> <EditText android:id="@+id/edit_item_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/button_upload_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上传图片" /> <TextView android:id="@+id/text_image_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="图片路径:" /> <Button android:id="@+id/button_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout>
05-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值