2017-08-28 29 views
-5

我必须创建一个显示当前日期的应用程序。在向前滑动日期应递增,并且向后滑动日期应递减。如何在android中增加和减少日期?

我成功地向前滑动显示当前日期和增量。但我不会在单一活动中如何减少向后滑动。

这是我的代码。

TAB活动

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
     Calendar c = Calendar.getInstance(); 
     c.add(Calendar.DAY_OF_MONTH,position); 
     String dte = sdf.format(c.getTime()).toString(); 
     TextView textView2 = getView().findViewById(R.id.textView2); 
     textView2.setText(dte); 
    } 
} 
+0

如果我要建立这个应用程序,那么我会用任何Android的GitHub日期选择器库。或者利用这个[采集器,阅读谷歌开发人员文档的采摘](https://developer.android.com/guide/topics/ui/controls/pickers.html) –

回答

0
int daysToDecrement = -1; 
c.add(Calendar.DATE, daysToDecrement); 

这将减一日期。 所以根据你的backwardswipe你必须减少天数。

0

只不过是你需要改变你的模式布局日期选取器微调器的增量和损害,

android:datePickerMode="spinner"

0

使用Calendar.DATE代替Calendar.DAY_OF_MONTH,并通过您要多少天增加或减少的位置。例如增加两天使用2和减少两天使用-2

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
Calendar c = Calendar.getInstance(); 
c.add(Calendar.DATE, position); 
String dte = sdf.format(c.getTime()).toString();