gpt4 book ai didi

java - 在Java文件中添加ID

转载 作者:行者123 更新时间:2023-12-01 04:38:21 26 4
gpt4 key购买 nike

我已经在 J​​ava 文件中创建了 datePicker 对话框。这是代码:

public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker

final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {

mDateDisplay .setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also
}
}

我想将 ID 添加到我的 DatePicker。我怎样才能从Java文件中做到这一点?

最佳答案

检查一下:

static final int DATE_DIALOG_ID = 999;
showDialog(DATE_DIALOG_ID);

覆盖以下内容:

protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
}

我还需要一个监听器:

    private DatePickerDialog.OnDateSetListener datePickerListener 
= new DatePickerDialog.OnDateSetListener() {

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;

//set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));

// set selected date into datepicker also
dpResult.init(year, month, day, null);

}
};

关于java - 在Java文件中添加ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17009311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com