DatePicker:
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | android.widget.DatePicker |
DatePicker是一个选择日期的控件。
与DataPicker相似的一个控件是TimePicker,用法相同.
如何使用DatePicker:
1、先写一个布局,该布局中包含一个TextView用来显示日期,另有一个Button,用来显示DatePicker控件。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView android:id="@+id/dateDisplay"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=""/>
- <Button android:id="@+id/pickDate"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="改变日期"
- />
- </LinearLayout>
2、产生一个DatePicker的对话框,在主activity中:
- public class DatePickerDemo extends Activity {
- private TextView text = null;
- private Button button = null;
- //用来保存年月日:
- private int mYear;
- private int mMonth;
- private int mDay;
- //声明一个独一无二的标识,来作为要显示DatePicker的Dialog的ID:
- static final int DATE_DIALOG_ID = 0;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- text = (TextView)findViewById(R.id.dateDisplay);
- button = (Button)findViewById(R.id.pickDate);
- //给button添加事件监听器:
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- //调用Activity类的方法来显示Dialog:调用这个方法会允许Activity管理该Dialog的生命周期,
- //并会调用 onCreateDialog(int)回调函数来请求一个Dialog
- showDialog(DATE_DIALOG_ID);
- }
- });
- //获得当前的日期:
- final Calendar currentDate = Calendar.getInstance();
- mYear = currentDate.get(Calendar.YEAR);
- mMonth = currentDate.get(Calendar.MONTH);
- mDay = currentDate.get(Calendar.DAY_OF_MONTH);
- //设置文本的内容:
- text.setText(new StringBuilder()
- .append(mYear).append("年")
- .append(mMonth + 1).append("月")//得到的月份+1,因为从0开始
- .append(mDay).append("日"));
- }
- }
这时候当运行程序,点击按键时,会报错:
此时程序应该运行到activity调用showDialog方法,该方法会触发当前activity的onCreateDialog()回调函数的调用,所以我们应该重写当前activity的onCreateDialog()函数,并在其内实例化一个DatePickerDialog,实例化DatePickerDialog需要为其指定事件监听器,所以我们还要为其提供一个事件监听器,完整的activity代码如下:
- import java.util.Calendar;
- import android.app.Activity;
- import android.app.DatePickerDialog;
- import android.app.Dialog;
- import android.app.DatePickerDialog.OnDateSetListener;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.DatePicker;
- import android.widget.TextView;
- public class DatePickerDemo extends Activity {
- private TextView text = null;
- private Button button = null;
- //用来保存年月日:
- private int mYear;
- private int mMonth;
- private int mDay;
- //声明一个独一无二的标识,来作为要显示DatePicker的Dialog的ID:
- static final int DATE_DIALOG_ID = 0;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- text = (TextView)findViewById(R.id.dateDisplay);
- button = (Button)findViewById(R.id.pickDate);
- //给button添加事件监听器:
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- //调用Activity类的方法来显示Dialog:调用这个方法会允许Activity管理该Dialog的生命周期,
- //并会调用 onCreateDialog(int)回调函数来请求一个Dialog
- showDialog(DATE_DIALOG_ID);
- }
- });
- //获得当前的日期:
- final Calendar currentDate = Calendar.getInstance();
- mYear = currentDate.get(Calendar.YEAR);
- mMonth = currentDate.get(Calendar.MONTH);
- mDay = currentDate.get(Calendar.DAY_OF_MONTH);
- //设置文本的内容:
- text.setText(new StringBuilder()
- .append(mYear).append("年")
- .append(mMonth + 1).append("月")//得到的月份+1,因为从0开始
- .append(mDay).append("日"));
- }
- //需要定义弹出的DatePicker对话框的事件监听器:
- private DatePickerDialog.OnDateSetListener mDateSetListener =new OnDateSetListener() {
- public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
- mYear = year;
- mMonth = monthOfYear;
- mDay = dayOfMonth;
- //设置文本的内容:
- text.setText(new StringBuilder()
- .append(mYear).append("年")
- .append(mMonth + 1).append("月")//得到的月份+1,因为从0开始
- .append(mDay).append("日"));
- }
- };
- /**
- * 当Activity调用showDialog函数时会触发该函数的调用:
- */
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DATE_DIALOG_ID:
- return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);
- }
- return null;
- }
- }
这样,成功运行,效果如下:
3、还有另一种方式产生DatePicker的对话框,这种方式的思路是将DatePicker控件单独作为一个activity,并由主activity对其发出请求,当选择好日期后,用户点击确定键时,DatePicker的activity会将日期返回给主activity。但是我们希望DatePicker以对话框的形式出现,这就需要我们修改DatePicker的activity的风格属性android:theme
看例子:
首先新建一个activity:如何建:在资源文件的layout目录下定义一个xml文件作为布局文件(datepicker.xml):
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <DatePicker
- android:id="@+id/myDatePicker"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <Button android:id="@+id/mybutton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="确定"
- />
- </LinearLayout>
定义一个类(DataPickerActivity)继承自Activity,重写其onCreate()函数,并在其内部加载布局文件:
- public class DatePickerActivity extends Activity {
- private Button button = null;
- private DatePicker datePicker = null;
- //用来保存年月日:
- private int mYear;
- private int mMonth;
- private int mDay;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.datepicker);
- button = (Button)findViewById(R.id.mybutton);
- datePicker = (DatePicker)findViewById(R.id.myDatePicker);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- //待添加
- }
- });
- }
- }
在AndroidManifest.xml文件中声明新的Activity:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.tjx"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".DatePickerDemo"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".DatePickerActivity"
- android:label="@string/app_name"
- android:theme = "@android:style/Theme.Dialog">
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="7" />
- </manifest>
注意DatePickerActivity的<activity>元素有一个属性android:theme = "@android:style/Theme.Dialog" 声明该activity以对话框形式出现。这时候我们可以在主activity中发送一个intent来获取结果,这里要用到Activity的startActivityForResult()获得结果数据,我引用了些别人的东西来说明获得结果数据的方法步骤:
有两个Activity,A,B,由A启动B,并传数据给B,B在经过处理后把数据传回给A。举个例子,A是主程序,B是设置参数的Activity,在B修改设置后,要让A重新读取设置,就要用到回传数据.主要是两个函数startActivityForResult onActivityResult
先是A传B:
- Bundle bd = new Bundle();
- bd.putString("Dir", "/sdcard/");
- Intent intent=new Intent();
- intent.putExtras(bd);
- intent.setClass(main.this,location.class);
- startActivityForResult(intent,2);
- //这里的2是我随便写的,代表requestCode,就是用来做个标记,网上的例子好像都是写requestCode,
- //结果我在测试时A没接收到,这里必须填正数
然后就是B接收再传回:
- Intent it = new Intent();
- it.putExtra("Dir",(String) tv1.getText());
- setResult(2, it);//2与前面的A里的2对应
- finish();
A里的接收代码:
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- //B返回时触发
- }
接下来修改我们的代码
在主activity中需要调用startActivityForResult 函数来发送请求intent,并重写onActivityResult回调函数来获得返回的结果:
相应的,DatePickerActivity也要做修改,返回结果:
- public class DatePickerActivity extends Activity {
- private Button button = null;
- private DatePicker datePicker = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.datepicker);
- button = (Button)findViewById(R.id.mybutton);
- datePicker = (DatePicker)findViewById(R.id.myDatePicker);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.putExtra("year",datePicker.getYear()+"");
- intent.putExtra("month",datePicker.getMonth()+"");
- intent.putExtra("day",datePicker.getDayOfMonth()+"");
- DatePickerActivity.this.setResult(1000, intent);
- finish();//必须手动finish
- }
- });
- }
- }
运行效果如下: