我按照本教程制作应用程序 - Link 我的问题是如何为两个不同的按钮添加两个日期选择器?在我的情况下,例如:一个用于开始日期,一个用于结束日期。
代码更新 - (初始化变量)
private System.Boolean sd;
private System.Boolean ed;
private const int Start_Date = 1;
private int SDYear = 2018, SDMonth = 1, SDDay = 1;
private const int End_Date = 2;
private int EDYear = 2018, EDMonth = 1, EDDay = 1;
start_date = FindViewById<EditText>(Resource.id.start_date_et)
end_date = FindViewById<EditText>(Resource.id.end_date_et)
(click events )
Start_date.Click += delegate {
sd = true;
ed = false;
ShowDialog(Resource.Id.start_date_et);
};
End_date.Click += delegate {
ed = true;
sd = false;
ShowDialog(Resource.Id.end_date_et);
};
(覆盖对话框的方法)
protected override Dialog OnCreateDialog(int id)
{
switch (id)
{
case (Resource.Id.start_date_et):
{
return new DatePickerDialog(this, Resource.Style.DialogTheme, this, SDYear, SDMonth, SDDay);
break;
}
case (Resource.Id.end_date_et):
{
return new DatePickerDialog(this, Resource.Style.DialogTheme, this, EDYear, EDMonth, EDDay);
break;
}
default:
break;
}
return null;
}
(ondataset界面)
public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
if(sd == true){
this.SDYear = year;
this.SDMonth = month;
this.SDDay = dayOfMonth;
start_date_text = (SDMonth + 1).ToString() + "-" + SDDay.ToString() + "-" + SDYear.ToString();
Toast.MakeText(this, (SDMonth + 1)
+ "-" + (SDDay) + "-" + SDYear, ToastLength.Short).Show();
}else if(ed == true){
this.EDYear = year;
this.EDMonth = month;
this.EDDay = dayOfMonth;
end_date_text = (EDMonth + 1).ToString() + "-" + EDDay.ToString() + "-" + EDYear.ToString();
Toast.MakeText(this, (EDMonth + 1)
+ "-" + (EDDay) + "-" + EDYear, ToastLength.Short).Show();
}
}
我想通过这种方法,但每当我尝试在textview上显示start_date_text和end_date_text时,start_date_text或end_date_text都会显示,但不会同时显示。
答案 0 :(得分:2)
日期选择器的cs代码
var fragView = this.BindingInflate(Resource.Layout.yourView, null);
dateselect = fragView.FindViewById<EditText>(Resource.Id.txtdeliverydateselect);
dateselect.Click += delegate
{
DateTime currentdate;
currentdate = DateTime.Now.ToLocalTime();
var dialog = new DatePickerDialogFragment(Activity, currentdate, this);
dialog.Show(FragmentManager, "date");
};
你的axml代码
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="360dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<EditText
android:id="@+id/txtdeliverydateselect"
android:hint="Monday 31 October"
android:layout_width="fill_parent"
android:layout_weight="0.9"
android:textSize="18dp"
/>
</LinearLayout>
使用与其他日期选择器相同的用户
答案 1 :(得分:0)
在OnDateSet方法中,您要为日期分配文本值而不添加任何条件,您应该使用 id 检查单击哪个按钮,并根据该ID更新StartDate TextView和EndDate TextView。
public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
if(StartDateIdClicked){
this.SDYear = year;
this.SDMonth = month;
this.SDDay = dayOfMonth;
start_date_text = (SDMonth + 1).ToString() + "-" + SDDay.ToString() + "-" + SDYear.ToString();
Toast.MakeText(this, (SDMonth + 1)
+ "-" + (SDDay) + "-" + SDYear, ToastLength.Short).Show();
}
elseif(EndDateIdClicked){
this.EDYear = year;
this.EDMonth = month;
this.EDDay = dayOfMonth;
end_date_text = (EDMonth + 1).ToString() + "-" + EDDay.ToString() +"-" + EDYear.ToString();
Toast.MakeText(this, (EDMonth + 1)
+ "-" + (EDDay) + "-" + EDYear, ToastLength.Short).Show();
}
}
当您调用ShowDialog(Start_Date)时,您为 End_Date = 1 和 Start_Date = 1 分配相同值;和ShowDialog(End_Date); 然后两个OnCreateDialog(),切换情况都是真的 Start_Date和End_Date