禁用datepicker中的递减按钮

时间:2013-04-30 12:04:31

标签: android android-datepicker

我想在Android中开发预订应用程序。对于日期字段,我使用的是datePickerDialog。但是,我想在达到当前日期时禁用递减按钮,以便用户过去无法进行预约。

你知道怎么做吗?

这是我的活动课程:

public class DatePickerActivity extends Activity {

private int mYear;
private int mMonth;
private int mDay;


private TextView mDateDisplay;
private Button mPickDate;

static final int DATE_DIALOG_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mDateDisplay = (TextView) findViewById(R.id.showMyDate);        
    mPickDate = (Button) findViewById(R.id.myDatePickerButton);

    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });


    // get the current date
    final Calendar c = Calendar.getInstance();

    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);


    // display the current date
    updateDisplay();

}
private void updateDisplay() {

    String selectedDate = mDay+"-"+ mMonth + 1 +  "-"+mYear;

    this.mDateDisplay.setText(
        new StringBuilder()
                .append(mDay).append("-")
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")

                .append(mYear).append(" "));
}

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

1 个答案:

答案 0 :(得分:0)

使用您的日历对象比较日期值并在日期更改日历中设置值

Calendar c;

oncreate(){
// initialize Calendar object
}

private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {

            Date date = new Date();
            c = Calendar.getIntance(); // here you need to create new instance of calendar because its time was older
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            c.set(mYear, mMonth, mDay);

            // now check here your Calendar date was after current date then update
            // otherwise do nothing

            if(c.getTime().getTime()>date.getTime()){ // updated condition
                updateDisplay();
            }
        }
    };