我使用兩個日期選取器在我的活動,並試圖從管理拾取日期......但是,這是正在發生的事情在選擇器的日期是不一樣的,我想設置的日期... 的代碼是:
public class SignInAsEmployee extends Activity implements OnClickListener,
OnFocusChangeListener {
EditText UserID, et_from, et_to;
Button showinfo;
static final int DATE_DIALOG_ID_From = 1;
static final int DATE_DIALOG_ID_TO = 0;
int day_q, month_q, year_q;
int day_p, month_p, year_p;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signinas_employee);
UserID = (EditText) findViewById(R.id.et_employeeId);
showinfo = (Button) findViewById(R.id.btn_submit);
et_from = (EditText) findViewById(R.id.tv_from);
et_to = (EditText) findViewById(R.id.tv_to);
showinfo.setOnClickListener(this);
et_from.setOnFocusChangeListener(this);
et_to.setOnFocusChangeListener(this);
}
@Override
public void onClick(View v) {
String uId = UserID.getText().toString().trim();
Intent ShowIinfo = new Intent(SignInAsEmployee.this, MainActivity.class);
ShowIinfo.putExtra("userId", uId);
startActivity(ShowIinfo);
}
@SuppressWarnings("deprecation")
@Override
public void onFocusChange(View arg0, boolean arg1) {
switch (arg0.getId()) {
case R.id.tv_from:
if (this.et_from.isFocused()) {
showDialog(DATE_DIALOG_ID_From);
}
break;
case R.id.tv_to:
if (this.et_to.isFocused()) {
showDialog(DATE_DIALOG_ID_TO);
}
break;
}
}
private DatePickerDialog.OnDateSetListener from_dateListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
day_q = selectedDay;
month_q = selectedMonth + 1;
year_q = selectedYear;
et_from.setText(day_q + " - " + month_q + "-" + year_q);
}
};
private DatePickerDialog.OnDateSetListener to_dateListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
day_p = selectedDay;
month_p = selectedMonth + 1;
year_p = selectedYear;
et_to.setText(day_p + " - " + month_p + "-" + year_p);
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID_From:
return new DatePickerDialog(this, from_dateListener, year_q,
month_q, day_q);
case DATE_DIALOG_ID_TO:
return new DatePickerDialog(this, to_dateListener, year_p, month_p,
day_p);
}
return null;
}
}
No Bro month_q = selectedMonth + 1;用於精確設置日期,否則如果您選擇11作爲月份,它會向您顯示10 ...我知道這不是解決方案 – NRahman