Android 开发笔记___DatePicker__日期选择器
虽然EditText提供了inputTtype=“date”,但用户往往不太喜欢自己输入时间。
Android为这个提供了DatePicker,但有很多缺点,不是弹窗模式,而是直接在页面上占据一块区域,并且不会自动关闭。
因此它不适合直接使用,在开发中往往用已经封装好的日期选择对话框DatePickerDialog。
- 相当于在AlertDialog上加载了DatePicker
- 由监听器OnDateSetListener负责响应
- 在OnDateSet方法中获得用户选择的具体日期
- 当然,一月份对应的不是1,是0,你懂的!
package com.example.alimjan.hello_world; import java.util.Calendar; /**
* Created by alimjan on 7/15/2017.
*/
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView; import com.example.alimjan.hello_world.two.class__2_1_2; public class class_5_1_1 extends AppCompatActivity implements
OnClickListener, OnDateSetListener { private TextView tv_date; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_5_1_1);
tv_date = (TextView) findViewById(R.id.tv_date);
findViewById(R.id.btn_date).setOnClickListener(this);
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_date) {
Calendar calendar = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(this, this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
} @Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String desc = String.format("您选择的日期是%d年%d月%d日",
year, monthOfYear+1, dayOfMonth);
tv_date.setText(desc);
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_5_1_1.class);
mContext.startActivity(intent);
} }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" > <Button
android:id="@+id/btn_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择日期"
android:textColor="@color/black"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="17sp" /> </LinearLayout>
Android 开发笔记___DatePicker__日期选择器的更多相关文章
- Android 开发笔记___时间选择器---timePicker
像datepicker一样,也有timepicker. 同样有timepickerdialog 所用到的方法还是一样,监听时间选择器的变化. package com.example.alimjan.h ...
- 【转】Android开发笔记(序)写在前面的目录
原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
- [置顶] Android开发笔记(成长轨迹)
分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...
- Android开发笔记:打包数据库
对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...
- android开发时间和日期的代码实现工具类(一)
android开发时间和日期工具类的代码实现: package com.gzcivil.utils; import android.annotation.SuppressLint; import an ...
- Android开发笔记--hello world 和目录结构
原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...
- [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明
接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...
- [APP] Android 开发笔记 002-命令行创建默认项目结构说明
接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.
- Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计
Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...
随机推荐
- String的replace和replaceAll
replace(CharSequence target, CharSequence replacement) 这里CharSequence是一个接口 实现类包括CharBuffer, Segement ...
- BP算法
1986年Rumelhart和McCelland在<并行分布式处理>中提出了BP算法,即非线性连续变换函数的多层感知器网络误差反向传播算法. 该算法的思想是:学习过程分为信号的正向传播与误 ...
- 退出psql时,报psql_history的错
数据库版本:Enterprisedb 9.2(postgreSQL) 错误如下所示: postgres=# exitcould not save history to file "/opt/ ...
- ubuntu环境下lnmp环境搭建(3)之Php
1.lnmp详细 http://www.discuz.net/thread-3513107-1-1.html 2. 到php目录 http://blog.aboutc.net/linux/65/co ...
- TreeViewItem实现整行选中 (两种用法)
用法一 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quo ...
- 数组去重方法(ES6)
let arrayBefore = [1,3,3,2,1,5,2,1]; //去重之前的数组 Array.prototype.dedupe = function (){ //去重函数 返回去重后的数组 ...
- 极化码之tal-vardy算法(3)
考完驾照,回来填坑 /doge/doge 前两节分别介绍了tal算法中的合并函数和信道操作两个部分,我们将高斯信道的应用放在最后一节来介绍. 在之前的介绍中,我们一直在一个前提下进行讨论--即输入字符 ...
- CSS组件
下拉菜单 .dropdown:将下拉菜单触发器和下拉菜单包含在其中 .dropdown-menu:给<ul>制定下拉菜单的样式 .dropup:向上弹出菜单 .dropdown-menu- ...
- 基于HTML5的WebGL实现的2D3D迷宫小游戏
为了实现一个基于HTML5的场景小游戏,我采用了HT for Web来实现,短短200行代码,我就能实现用"第一人称"来操作前进后退上下左右,并且实现了碰撞检测. 先来看下实现的效 ...
- Centos7安装后出现please make your choice from '1' to e 解决方式
[输入"1",按Enter键 输入"2",按Enter键 输入"q",按Enter键 输入"yes",按 ...