2014-02-20 261 views
1

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

} 

回答

0

我從你的代碼猜你在這裏做什麼錯在兩個DatePickerDialog.OnDateSetListener

month_q = selectedMonth + 1; 

應該是month_q = selectedMonth; ......我不知道,但你可以嘗試...

+0

No Bro month_q = selectedMonth + 1;用於精確設置日期,否則如果您選擇11作爲月份,它會向您顯示10 ...我知道這不是解決方案 – NRahman

0

你傳遞給DatePickerDialog構造函數的初始日,月和年的值沒有被初始化,並且在一個範圍中,這個小部件並不真正支持。您應該提供合理的默認值,例如Calendar.getInstance()即當前日期。

相關問題