gpt4 book ai didi

java - 为什么我的代码使用监听器将值设置为错误的 View

转载 作者:行者123 更新时间:2023-12-01 15:44:25 24 4
gpt4 key购买 nike

我有四个按钮,显示日历的日期/时间,这是我的 Activity 的属性。当按下按钮时,相应的 DatePickerDialog 被调用,它设置用户在日历对象中输入的值。

尽管日历和对话框的标识似乎是正确的,但当我按“endDate”并单击对话框中的“确定”时,startDate 的文本就会被设置。

为什么 Android 会有这样的行为并修复这种无意义的行为? (是的,我清理并刷新了项目......)

预先感谢,你们总是给我带来惊喜:)

更新好的,现在我明白了,我在 onTimeSetListener fr startDate 中设置了月份而不是分钟;修复了代码/更新

这是列表:这是属性定义的一部分(我省略了 View )

static final int START_DATE_DIALOG_ID   = 0;
static final int START_TIME_DIALOG_ID = 1;
static final int END_DATE_DIALOG_ID = 2;
static final int END_TIME_DIALOG_ID = 3;

这是onCreate()的相关部分

startDate = (Button)findViewById(R.id.startDate);
startTime = (Button)findViewById(R.id.startTime);
endDate = (Button)findViewById(R.id.endDate);
endTime = (Button)findViewById(R.id.endTime);
startCal = Calendar.getInstance();
endCal = Calendar.getInstance();
[......]
startDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(START_DATE_DIALOG_ID);
}
});
startTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(START_TIME_DIALOG_ID);
}
});
endDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(END_DATE_DIALOG_ID);
}
});
endTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(END_TIME_DIALOG_ID);
}
});


@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case START_DATE_DIALOG_ID:
return new DatePickerDialog(this,
startDateSetListener,
startCal.get(Calendar.YEAR),
startCal.get(Calendar.MONTH),
startCal.get(Calendar.DAY_OF_MONTH));
case START_TIME_DIALOG_ID:
return new TimePickerDialog(this,
startTimeSetListener,
startCal.get(Calendar.HOUR_OF_DAY),
startCal.get(Calendar.MINUTE),
true); // is24hView
case END_DATE_DIALOG_ID:
return new DatePickerDialog(this,
endDateSetListener,
endCal.get(Calendar.YEAR),
endCal.get(Calendar.MONTH),
endCal.get(Calendar.DAY_OF_MONTH));
case END_TIME_DIALOG_ID:
return new TimePickerDialog(this,
endTimeSetListener,
endCal.get(Calendar.HOUR_OF_DAY),
endCal.get(Calendar.MINUTE),
true); // is24hView
}
return null;
}


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

public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
startCal.set(Calendar.YEAR, year);
startCal.set(Calendar.MONTH, monthOfYear);
startCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;
private DatePickerDialog.OnDateSetListener endDateSetListener =
new DatePickerDialog.OnDateSetListener() {

public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
endCal.set(Calendar.YEAR, year);
endCal.set(Calendar.MONTH, monthOfYear);
endCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;
private TimePickerDialog.OnTimeSetListener startTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
startCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
startCal.set(Calendar.MINUTE, minute);
updateDisplay(START_TIME_DIALOG_ID);
}
}
;
private TimePickerDialog.OnTimeSetListener endTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
endCal.set(Calendar.HOUR_OF_DAY, hourOfDay);
endCal.set(Calendar.MINUTE, minute);
updateDisplay(END_TIME_DIALOG_ID);
}
}
;

// updates the date in the TextView
private void updateDisplay(int dialogId) {
switch(dialogId){
case START_DATE_DIALOG_ID:
startDate.setText(getDateFromCal(startCal));
break;
case START_TIME_DIALOG_ID:
startTime.setText(getTimeFromCal(startCal));
break;
case END_DATE_DIALOG_ID:
endDate.setText(getDateFromCal(endCal));
break;
case END_TIME_DIALOG_ID:
endTime.setText(getTimeFromCal(endCal));
break;
}
}

private String getDateFromCal(Calendar cal){
return new StringBuilder()
.append(cal.get(Calendar.DAY_OF_MONTH)) .append(".")
.append(cal.get(Calendar.MONTH) + 1) .append(".")
.append(cal.get(Calendar.YEAR)) .toString();
}
private String getTimeFromCal(Calendar cal){
StringBuilder sb = new StringBuilder();
int num = cal.get(Calendar.HOUR_OF_DAY);
if(num<10)
sb.append("0");
sb.append(num);
sb.append(":");
num = cal.get(Calendar.MINUTE);
if(num<10)
sb.append("0");
sb.append(num);
return sb.toString();
}

最佳答案

您从监听器 endDateSetListener 更新了错误的显示 (START_DATE_DIALOG_ID)。

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

public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
endCal.set(Calendar.YEAR, year);
endCal.set(Calendar.MONTH, monthOfYear);
endCal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(START_DATE_DIALOG_ID);
}
}
;

另外 - 我可以提出建议吗?很少有语言或库会出现问题。永远不要假设它是。 Jeff Atwood(StackOverflow 的创始人之一)有一篇关于这个主题的优秀文章:First rule of programming: It's always your fault

关于java - 为什么我的代码使用监听器将值设置为错误的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7380146/

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