阅读《Android 从入门到精通》(13)——日期选择器
Posted SweetLoverFT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阅读《Android 从入门到精通》(13)——日期选择器相关的知识,希望对你有一定的参考价值。
日期选择器(DatePicker)
java.lang.Object;
android.view.View;
android.view.ViewGroup;
android.widget.FrameLayout;
android.widget.DataPicker;
DatePicker 类方法
DatePicker 示例
完整工程:http://download.csdn.net/detail/sweetloveft/9410380
下述程序主要学习 DatePicker 和 Calendar 的使用方法,需注意 Eclipse 似乎不能显示 DatePicker 的预览,但可以正常工作的,这一点已查阅 StackOverflow。
1.MainActivity.java
package com.sweetlover.activity;
import com.sweetlover.datepickerdemo.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
public class MainActivity extends Activity
private TextView textView = null;
// private Calendar calendar = null; // 不使用 Calendar 是因为它会显示出来,和 DatePicker 冲突
private DatePicker datePicker = null;
private int curYear, curMonth, curDay;
private class DateChangedListener implements OnDateChangedListener
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth)
// TODO Auto-generated method stub
curYear = year;
curMonth = monthOfYear;
curDay = dayOfMonth;
textView.setText("当前时间:" + curYear + "年" + curMonth + "月" + curDay + "日");
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView1);
datePicker = (DatePicker)findViewById(R.id.datePicker1);
curYear = 2016;
curMonth = 1;
curDay = 19;
// calendar = Calendar.getInstance();
// curYear = calendar.get(Calendar.YEAR);
// curMonth = calendar.get(Calendar.MONTH) + 1;
// curDay = calendar.get(Calendar.DAY_OF_MONTH);
textView.setText("当前时间:" + curYear + "年" + curMonth + "月" + curDay + "日");
datePicker.init(curYear, curMonth, curDay, new DateChangedListener());
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp"
android:orientation="vertical" >
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
3.AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sweetlover.datepickerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.sweetlover.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
以上是关于阅读《Android 从入门到精通》(13)——日期选择器的主要内容,如果未能解决你的问题,请参考以下文章