ANDROID_MARS学习笔记_S02_002_Date\TimePicker
一、文档用法
1.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.s02_e02_dateandtimepickerdialog.MainActivity" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pick_date"
android:onClick="showDatePickerDialog" /> </RelativeLayout>
2.java
package com.example.s02_e02_dateandtimepickerdialog; import java.util.Calendar; import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker; @SuppressLint("NewApi")
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
} @SuppressLint("NewApi")
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) {
// Do something with the date chosen by the user
}
} }
二、教程用法
1.xml
<?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:id="@+id/textViewId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/showDatePickerButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示DatePicker"
/> </LinearLayout>
2.java
package mars.dateandtime; import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker; public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button showDatePickerButton = null;
//该常量用于标识DatePickerDialog
private static final int DATE_PICKER_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDatePickerButton = (Button) findViewById(R.id.showDatePickerButton);
showDatePickerButton.setOnClickListener(new ButtonListener());
} private class ButtonListener implements OnClickListener { @Override
public void onClick(View v) {
//此方法用于显示DatePickerDialog
showDialog(DATE_PICKER_ID);
} }
//监听器,用户监听用户点下DatePikerDialog的set按钮时,所设置的年月日
DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
System.out.println(year + "-" + monthOfYear + "-" + dayOfMonth);
}
}; @Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
return new DatePickerDialog(this, onDateSetListener, 2010, 11, 25);
}
return null;
} }
ANDROID_MARS学习笔记_S02_002_Date\TimePicker的更多相关文章
- ANDROID_MARS学习笔记_S01_010日期时间控件
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- ANDROID_MARS学习笔记_S01_012_RatingBar
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- ANDROID_MARS学习笔记_S01_012_SeekBar
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- ANDROID_MARS学习笔记_S01_011ProgressBar
文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...
- ANDROID_MARS学习笔记_S01_009Relative_LAYOUT例子
1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android ...
- ANDROID_MARS学习笔记_S01_008Linear_layout例子
1.netstone_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置
一.介绍 二.1.linear_layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...
- ANDROID_MARS学习笔记_S01_006ImageView
一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...
- ANDROID_MARS学习笔记_S01_005CheckBox
一. 1.checkbox_layout.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...
随机推荐
- SQL SERVER 主键约束
主键约束: 遵循关系型模型中的第二范式.唯一的识别一条记录,不能为空. CREATE TABLE Persons ( Id_P int NOT NULL PRIMARY KEY, LastName v ...
- Python获取本机的mac,ip,name
Python获取mac 获取计算机名字和ip(内网ip) 指定网卡ip
- Jquery Ajax Get示例
$.ajax({ type: "GET", url:"ajax_url.php", cache: false, data:{'action':'ABC',' ...
- iOS开发,多个button数组,每个数组只能选中5项,多个数组只能选择3个。
由于常用xib,所以不想用代码写那么多个button.而且也懒的算位置 直接xib拉线成四个数组.水果,零食,饮料,甜点. 入题实现的功能就是,在这四个数组之中只能在3个数组只选中5项.有点绕(就比如 ...
- [数据结构] 成都磨子桥技工学校 Challenge Series
Challenge 0: 第一次裸写竟然WA了.... Challenge 1: 不想写了.
- 学习C++ Primer 的个人理解(零)
由于自己看书之后经常容易忘记细节,所以写在博客上让我自己的思路更加清晰一些. 我的博客仅仅只是将书中的内容概括一下,有一些不好理解的部分我会用我自己理解的方式使用大白话写出来. 仅此而已.
- 转载:一句代码改变Swing难看的字体
Swing 皮肤的一个键值:swing.boldMetal 默认为 true因此造成了默认字体极度难看: 其实一句代码就能解决问题:UIManager.put("swing.boldMeta ...
- win7环境下配置Java环境
==下载Java SE Development Kit 8u45== http://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...
- php中utf8 与utf-8 与utf8 无BOM
utf8 与utf-8 相信很多程序员刚开始也会有这样的疑惑,如题,我也是. 其实,他们可以这样来区分. 一.在php和html中设置编码,请尽量统一写成“UTF-8”,这才是标准写法,而 ...
- 2014年度辛星html教程夏季版第五节
如果读者是一位后台开发者,那么肯定会知道什么叫表单,这里我们就介绍一下前台如何使用表单,表单的使用也是我们编写网页的必须经历的一关,而且,表单也往往是我们网站的漏洞和弱点出现的地方. ******** ...