日期选择日期和所选日期不匹配?

时间:2014-02-20 13:07:01

标签: android datepicker

enter image description here

我在我的活动中使用了两个日期选择器,并尝试从选择器管理日期...但是这是发生的事情,选择器的日期与我试图设置的日期不同... 代码是:

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;
    }

}

2 个答案:

答案 0 :(得分:0)

我想从你的代码中你在DatePickerDialog.OnDateSetListener

中做错了
month_q = selectedMonth + 1;

它应该是month_q = selectedMonth; ......我不确定但你可以试试......

答案 1 :(得分:0)

传递给DatePickerDialog构造函数的初始日,月和年值未初始化,并且位于窗口小部件不支持的范围内。您应该提供合理的默认值,例如来自Calendar.getInstance()的默认值,即当前日期。