首先,我只是一个初学者,但到目前为止我几乎完全了解我的应用程序,所以我希望有人能给我答案如何做到这一点。
这是我的完整应用代码:
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();
}
它告诉用户他们不能计划当前日期之前的日期,但我也希望将datepicker重置为当前日期,但我不知道如何。我再也不能使用“cal.set”了。
答案 0 :(得分:0)
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePicker mDpDate = (DatePicker) findViewById(R.id.dpDate);
// set current date into datepicker
mDpDate .init(year, month, day, null);