首先我只是一个初学者,但到目前为止我几乎完全了解我的应用程序,所以我希望有人能给我答案如何做到这一点。
这是我的完整应用程序代码:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.view.View;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import android.widget.Toast;
public class MainActivity extends Activity {
Button mBtnPlanMe;
CheckBox mCbSaturdaysAllowed;
DatePicker mDpDate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamiclayout);
mBtnPlanMe = (Button) findViewById(R.id.btnPlanMe);
mCbSaturdaysAllowed = (CheckBox) findViewById(R.id.cbSaturdaysAllowed);
mDpDate = (DatePicker) findViewById(R.id.dpDate);
}
public void PlanMe(View view){
if (view == mBtnPlanMe){
Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
int xday = mDpDate.getDayOfMonth();
int xmonth = mDpDate.getMonth();
int xyear = mDpDate.getYear();
cal.set(xyear, xmonth, xday);
if (cal.before(cal1) == true) {
Toast.makeText(this, "Kan geen datum inplannen voor vandaag", Toast.LENGTH_LONG).show();
}
else {
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
cal.add(Calendar.DAY_OF_MONTH, 1);
}
if (!mCbSaturdaysAllowed.isChecked() && cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
cal.add(Calendar.DAY_OF_MONTH, 2);
}
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String date = formatter.format(cal.getTime());
Toast.makeText(this, "Uw afspraak is ingepland op: " + date, Toast.LENGTH_LONG).show();
}
}
}
}
现在,在这部分:
if (cal.before(cal1) == true) {
Toast.makeText(this, "Cannot plan a date earlier than today", Toast.LENGTH_LONG).show();
}
它告诉用户他们无法计划当前日期之前的日期,但我也希望将日期选择器重置为当前日期,但我不知道如何。我不能再次使用“cal.set”。