0

如何在单个文本视图中显示星期几。

我有如下代码。在 logcat 中将星期日期显示为字符串。

    Calendar c = Calendar.getInstance();

    // Set the calendar to tuesday of the current week
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    c.add(Calendar.DATE, week * 5);
    c.add(Calendar.WEEK_OF_YEAR, week); //February
    // Print dates of the current week starting on Monday
    SimpleDateFormat df = new SimpleDateFormat("E dd yyyy", Locale.US);

    for (int i = 0; i < 5; i++) {

        //System.out.println(df.format(c.getTime()));
        String string= df.format(c.getTime());

        Log.d("", "MY YEAR OF MONTH=="+string); //[TUE 25 2014, WED 26 2014, THU 27 2014, FRI 28 2014, SAT 01 2014]
        c.add(Calendar.DAY_OF_MONTH, 1);
    //But this textview show only last date(sat 01 2014).
       textView.setText(string);

我的问题是如何在单个 texview 中显示所有星期日期。以上 textView.setText(string); 只显示最后一个日期,但我想在单个文本视图中显示整个星期的日期。

谢谢

4

1 回答 1

0

您可以使用两种方法。第一,使用该方法append(),第二,您可以将字符串变量中先前添加的值与新值连接起来。

前任:append()

for (int i = 0; i < 5; i++) {
        String string= df.format(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 1);
       textView.append(string);
}

例 2:连接

将 String 变量放在 for 循环之外并使用+=

String string = "";
for (int i = 0; i < 5; i++) {
    string += df.format(c.getTime());
    c.add(Calendar.DAY_OF_MONTH, 1);      
}
textView.setText(string);
于 2014-02-25T18:42:03.513 回答