【发布时间】:2013-12-05 13:05:01
【问题描述】:
这是我从日期选择器中选择日期的代码。
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
public class CurrentDateActivity extends Activity {
/** Called when the activity is first created. */
private EditText mDateDisplay,mDateSelect;
private int mYear,mMonth,mDay;
private int year,month,day;
Button selectBtn = null;
Calendar c = null;
static final int DATE_DIALOG_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_select);
selectBtn = (Button)findViewById(R.id.BtnSubmit);
mDateDisplay = (EditText) findViewById(R.id.currentDate);
mDateSelect = (EditText)findViewById(R.id.dateofBirth);
// add a click listener to the button
mDateSelect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
getCurrentDate();//get the current date
// display the current date
mDateDisplay.setText(getString(R.string.strSelectedDate,
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("/")
.append(mMonth + 1).append("/")
.append(mYear).append(" "))
);
}
private void getCurrentDate(){
// get the current date
c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
// updates the date in the EditText
private void updateDisplay() {
mDateSelect.setText(getString(R.string.strSelectedDate,
new StringBuilder()
.append(mDay).append("/")
.append(mMonth + 1).append("/")
.append(mYear).append(" "))
);
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
}
return null;
}
}
一切都很好,但现在我想同时获取当前日期和用户在另一个活动中输入的日期,以便计算它们之间的差异。但是如何通过意图的日期我无法理解。请帮助
【问题讨论】:
-
参考this问题
-
我不是 android 专业人士,但也许这篇文章可以帮助你:[如何在 Android 中将对象从一个活动传递到另一个活动][1] [1]:stackoverflow.com/a/2736612/1751277
标签: android date datepicker