咱們先在簡單地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.dateexp.MainActivity" >
<TextView
android:id="@+id/dispaly_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/boom_date"
android:text="xxxx-xx-xx"
android:textSize="35sp" />
<Button
android:id="@+id/boom_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/dispaly_date"
android:text="彈出"/>
</RelativeLayout>
將這樣顯示:
接下來是在Main_Activity.java文件裏對控件的操作:
package com.example.dateexp;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MainActivity extends Activity {
int tyear,tmonth,tday;//年月日
TextView display_date;
Button boom_date;
final int DATA_DIOLOG = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display_date = (TextView)findViewById(R.id.dispaly_date);
boom_date = (Button)findViewById(R.id.boom_date);
boom_date.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATA_DIOLOG);//顯示日期窗口
}
});
final Calendar ca = Calendar.getInstance();//實例個日曆
tyear = ca.get(Calendar.YEAR);
tmonth = ca.get(Calendar.MONTH);
tday = ca.get(Calendar.DAY_OF_MONTH);
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATA_DIOLOG:
return new DatePickerDialog(this, mdateListener, tyear, tmonth, tday);
}
return null;
}
// 設置日期 利用StringBuffer追加
public void display() {
display_date.setText(new StringBuffer().append(tyear).append("年").append(tmonth + 1).append("月").append(tday).append("日"));
}//textview控件顯示格式
//監聽(DatePickerDialog爲系統自帶日曆)
private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
tyear = year;
tmonth = monthOfYear;
tday = dayOfMonth;
display();//textview控件顯示
}
};
}
然後運行:
這就成功給Activity添加了一個簡單的日曆選擇器。
那我們怎麼使得APP一打開,TextView就顯示現在的日期呢?
直接在onCreate裏面添加以下代碼即可:
String DateString = DateFormat.getDateInstance().format(new Date());
display_date.setText(DateString);//默認顯示當前日期
部分如圖:
下面來記錄添加這個日曆時遇到的問題:
1.爲什麼要設置變量final int DATA_DIOLOG = 1?(從文中的showDialog(int id)、onCreateDialog(int id)解釋)
答:showDialog(int id):當你想要顯示一個對話框時,調用showDialog(int id) 方法並傳遞一個唯一標識這個對話框的整數。當對話框第一次被請求時,Android從你的Activity中調用onCreateDialog(int id),你應該在這裏初始化這個對話框Dialog。這個回調方法被傳以和showDialog(int id)相同的ID。所以這裏就對應了onCreateDialog(int id)方法的case後面的id。
onCreateDialog(int id)是什麼:當你使用這個回調函數時,Android系統會有效的設置這個Activity爲每個對話框的所有者,從而自動管理每個對話框的狀態並掛靠到Activity上。(所以當你使用這個函數的時候,它將掛起多個對話框,然後當你觸發的showDialog裏面的ID對應上其case後面的id,那麼相應的對話框將彈出。)
綜合上面兩個函數的解釋,那就可以解釋爲何定義int DATA_DIOLOG = 1了。它就是個窗口的標識。