gpt4 book ai didi

android - 同一 Activity 中的多个 DatePickers

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:55 26 4
gpt4 key购买 nike

我对 Android 平台完全陌生,在学习开发过程的同时一直在构建应用程序。

目前,我正在进行一项 Activity ,我需要在其中部署 2 个日期选择器。一个是“开始日期”,另一个是“结束日期”。我一直在关注 android 开发者页面上的 DatePicker 教程:http://developer.android.com/resources/tutorials/views/hello-datepicker.html

对于一个 DatePicker,它工作得很好。

现在我的问题是,当我为第二个日期选择器复制整个过程时,它在模拟器和手机上都显示得很好。但是无论我按哪个按钮来选择日期,只有第一个 TextView 会更新,而第二个 TextView 会继续显示当前日期。

代码如下:

package com.datepicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class datepicker extends Activity {

private TextView mDateDisplay;
private TextView endDateDisplay;
private Button mPickDate;
private Button endPickDate;
private int mYear;
private int mMonth;
private int mDay;

static final int START_DATE_DIALOG_ID = 0;
static final int END_DATE_DIALOG_ID = 0;


/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* capture our View elements for the start date function */
mDateDisplay = (TextView) findViewById(R.id.startdateDisplay);
mPickDate = (Button) findViewById(R.id.startpickDate);

/* add a click listener to the button */
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(START_DATE_DIALOG_ID);
}
});

/* get the current date */
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

/* display the current date (this method is below) */
updateStartDisplay();


/* capture our View elements for the end date function */
endDateDisplay = (TextView) findViewById(R.id.enddateDisplay);
endPickDate = (Button) findViewById(R.id.endpickDate);

/* add a click listener to the button */
endPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(END_DATE_DIALOG_ID);
}
});

/* get the current date */
final Calendar c1 = Calendar.getInstance();
mYear = c1.get(Calendar.YEAR);
mMonth = c1.get(Calendar.MONTH);
mDay = c1.get(Calendar.DAY_OF_MONTH);

/* display the current date (this method is below) */
updateEndDisplay();
}



private void updateEndDisplay() {
endDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));

}



private void updateStartDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));


}

/* 当用户在开始日期函数的对话框中“设置”日期时收到的回调 */

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

public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateStartDisplay();
}
};

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case START_DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
/* the callback received when the user "sets" the date in the dialog for the end date function */

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

public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateStartDisplay();
}
};

protected Dialog onCreateDialog1(int id) {
switch (id) {
case END_DATE_DIALOG_ID:
return new DatePickerDialog(this,
endDateSetListener,
mYear, mMonth, mDay);
}
return null;
}

请告知代码所需的更改。

最佳答案

我有一个解决方案,可以在不添加新对话框类型的情况下允许无限数量的日期字段。当用户单击其中一个按钮时,我会在启动 DatePickerDialog 之前注册当前正在修改的 TextView 和 Calendar。对话框的 OnDateSetListener 然后更新注册的 TextView 和日历。

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MultiDatePickerActivity extends Activity {

private TextView startDateDisplay;
private TextView endDateDisplay;
private Button startPickDate;
private Button endPickDate;
private Calendar startDate;
private Calendar endDate;

static final int DATE_DIALOG_ID = 0;

private TextView activeDateDisplay;
private Calendar activeDate;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multidatepicker);

/* capture our View elements for the start date function */
startDateDisplay = (TextView) findViewById(R.id.startDateDisplay);
startPickDate = (Button) findViewById(R.id.startPickDate);

/* get the current date */
startDate = Calendar.getInstance();

/* add a click listener to the button */
startPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(startDateDisplay, startDate);
}
});

/* capture our View elements for the end date function */
endDateDisplay = (TextView) findViewById(R.id.endDateDisplay);
endPickDate = (Button) findViewById(R.id.endPickDate);

/* get the current date */
endDate = Calendar.getInstance();

/* add a click listener to the button */
endPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(endDateDisplay, endDate);
}
});

/* display the current date (this method is below) */
updateDisplay(startDateDisplay, startDate);
updateDisplay(endDateDisplay, endDate);
}

private void updateDisplay(TextView dateDisplay, Calendar date) {
dateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(date.get(Calendar.MONTH) + 1).append("-")
.append(date.get(Calendar.DAY_OF_MONTH)).append("-")
.append(date.get(Calendar.YEAR)).append(" "));

}

public void showDateDialog(TextView dateDisplay, Calendar date) {
activeDateDisplay = dateDisplay;
activeDate = date;
showDialog(DATE_DIALOG_ID);
}

private OnDateSetListener dateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
activeDate.set(Calendar.YEAR, year);
activeDate.set(Calendar.MONTH, monthOfYear);
activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(activeDateDisplay, activeDate);
unregisterDateDisplay();
}
};

private void unregisterDateDisplay() {
activeDateDisplay = null;
activeDate = null;
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
}
return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
break;
}
}
}

这种灵 active 在您直到运行时才知道需要多少日期选择器的应用程序中很有用。

关于android - 同一 Activity 中的多个 DatePickers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3734981/

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