问题 - 使用两个日期时间选择器
我正在尝试使用两个日期和时间选择器(例如 fromdate 和 To date)来获取选定的日期。如果我选择 fromdate,我可以将 fromdate 获取到文本视图。但是如果我选择第二个日期选择器(todate)更新相同的文本视图(Fromdate textview)。
public class F2Activity extends InfosoftActivity
{
private int mYear;
private int mMonth;
private int mDay;
private int mYear2;
private int mMonth2;
private int mDay2;
private TextView mDateDisplay;
private TextView mDateDisplay2;
private Button mPickDate;
private Button mPickDate2;
static final int DATE_DIALOG_ID = 0;
static final int DATE_DIALOG_IDD = 0;
public String AAA;
public String BBB;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Black);
setContentView(R.layout.activity_f2);
Button BtnView=(Button) findViewById(R.id.BtnView);
Button BtnDownload=(Button) findViewById(R.id.BtnDownload);
mDateDisplay = (TextView) findViewById(R.id.showMyDate);
mDateDisplay2 = (TextView) findViewById(R.id.showMyDatee);
mPickDate = (Button) findViewById(R.id.myDatePickerButton);
mPickDate2 = (Button) findViewById(R.id.myDatePickerButton2);
mPickDate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
showDialog(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
updateDisplay();
mPickDate2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
showDialog(DATE_DIALOG_IDD);
}
});
// get the current date
final Calendar c2 = Calendar.getInstance();
mYear2 = c2.get(Calendar.YEAR);
mMonth2 = c2.get(Calendar.MONTH);
mDay2 = c2.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplayTo();
private void updateDisplay()
{
this.mDateDisplay.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
private void updateDisplayTo()
{
this.mDateDisplay2.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth2 + 1).append("-")
.append(mDay2).append("-")
.append(mYear2).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;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener2 =new
DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int
monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
mYear2 = year;
mMonth2 = monthOfYear;
mDay2 = dayOfMonth;
updateDisplayTo();
}
};
protected Dialog onCreateDialog2 (int id)
{
switch (id)
{
case DATE_DIALOG_IDD:
return new DatePickerDialog(this,
mDateSetListener2,
mYear2, mMonth2, mDay2);
}
return null;
}
}
I'm trying to get selected dates using two date and time picker (like fromdate and To date). If I select fromdate I can get the fromdate to the textview. But if I select second datepicker (todate) update same textview (Fromdate textview).
public class F2Activity extends InfosoftActivity
{
private int mYear;
private int mMonth;
private int mDay;
private int mYear2;
private int mMonth2;
private int mDay2;
private TextView mDateDisplay;
private TextView mDateDisplay2;
private Button mPickDate;
private Button mPickDate2;
static final int DATE_DIALOG_ID = 0;
static final int DATE_DIALOG_IDD = 0;
public String AAA;
public String BBB;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Black);
setContentView(R.layout.activity_f2);
Button BtnView=(Button) findViewById(R.id.BtnView);
Button BtnDownload=(Button) findViewById(R.id.BtnDownload);
mDateDisplay = (TextView) findViewById(R.id.showMyDate);
mDateDisplay2 = (TextView) findViewById(R.id.showMyDatee);
mPickDate = (Button) findViewById(R.id.myDatePickerButton);
mPickDate2 = (Button) findViewById(R.id.myDatePickerButton2);
mPickDate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
showDialog(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
updateDisplay();
mPickDate2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
showDialog(DATE_DIALOG_IDD);
}
});
// get the current date
final Calendar c2 = Calendar.getInstance();
mYear2 = c2.get(Calendar.YEAR);
mMonth2 = c2.get(Calendar.MONTH);
mDay2 = c2.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplayTo();
private void updateDisplay()
{
this.mDateDisplay.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
private void updateDisplayTo()
{
this.mDateDisplay2.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth2 + 1).append("-")
.append(mDay2).append("-")
.append(mYear2).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;
updateDisplay();
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener2 =new
DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int
monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
mYear2 = year;
mMonth2 = monthOfYear;
mDay2 = dayOfMonth;
updateDisplayTo();
}
};
protected Dialog onCreateDialog2 (int id)
{
switch (id)
{
case DATE_DIALOG_IDD:
return new DatePickerDialog(this,
mDateSetListener2,
mYear2, mMonth2, mDay2);
}
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想在应用程序中使用 DatePicker 显示多个日期时间,则无需使用两次 DATE_DIALOG_ID。您只需使用一个即可满足您的要求。我在这里发布了代码,希望对您有所帮助。
在按钮中单击“
然后
然后
然后”
我希望它能解决您的问题。如果您对此有任何问题,可以问我。
If you want to display more than one time of date using DatePicker in your application then no need to use two time DATE_DIALOG_ID. You can use only one and fulfill your requirements. I posted here code I hope it will helps you.
In button click
Then
Then
Then
I hope it will solve your problem. If you have any problem regarding this then you can ask me.
检查我个人在我的代码中使用过的...它对我有用..
check this i have used in my code personally...it's working for me..