-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 に答える