-2
public class Main extends Activity {    
    protected static final int dialog = 0;
    private int Day;
    private int Month;
    private int Year;
    Button mbt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mbt = (Button) findViewById(R.id.button1);
        mbt.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            showDialog(dialog);
        }
        });
    }

    private void  UpdateDisplay() {
        String day = (DateFormat.format("EEEEEE", new Date(Year, Month, Day))).toString();
        Toast.makeText(Main.this, day, Toast.LENGTH_LONG).show();
    }

    private DatePickerDialog.OnDateSetListener 
    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
            Year = year;
            Month = monthOfYear;
            Day = dayOfMonth;
            UpdateDisplay();
        }
    };

    protected Dialog onCreateDialog(int id){
        switch(id) {
            case dialog:
                return new DatePickerDialog(this ,mDateSetListener ,Year , Month , Day);
        }
        return null;
    }
}
4

1 回答 1

2

当您选择一个日期时,Android 会将日期修改为您自己的时区,您应该Calendar使用创建一个类型变量,Calendar.getInstance(TimeZone.getDefault())并将其发出一个calendar.set(int year, int month, int day). 如果您不确定某事,请查看http://developer.android.com/reference/java/util/Calendar.html !:)

正确的代码是:

public class Main extends Activity {    
protected static final int dialog = 0;
private int Day;
private Calendar c;
private int Month;
private int Year;

    Button mbt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        c = new Calendar(TimeZone.getDefault());
        mbt = (Button) findViewById(R.id.button1);
        mbt.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            showDialog(dialog);
        }
        });
    }

    private void  UpdateDisplay() {
        String day = c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
        Toast.makeText(Main.this, day, Toast.LENGTH_LONG).show();
    }

    private DatePickerDialog.OnDateSetListener 
    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
            Year = year;
            Month = monthOfYear;
            Day = dayOfMonth;
            c.set(Year, Month, Day);
            UpdateDisplay();
        }
    };

    protected Dialog onCreateDialog(int id){
        switch(id) {
            case dialog:
                return new DatePickerDialog(this ,mDateSetListener ,Year , Month , Day);
        }
        return null;
    }
}
于 2013-08-07T13:45:08.323 回答