让 TextView 将星期几显示为字母而不是整数 [英] Have TextView show day of week as letters not integer

查看:25
本文介绍了让 TextView 将星期几显示为字母而不是整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本视图,它将星期几显示为整数 (0-7).我更喜欢它是否可以将其转换为字符串,然后可以将其显示在 TextView 中.我的代码如下.另外,我怎样才能让 TextViews 更新时间、日期等(它只显示应用程序打开的时间)?提前致谢.

I have a textview which shows the day of the week as an integer (0-7). I would prefer if it could convert that to a string, which could then be shown in a TextView. My code is below. Also, how can I make it so the TextViews update the time, date, etc. (it only shows the time the app is opened)? Thanks in advance.

MainActivity.java:

MainActivity.java:

package press.linx.calendar;

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.os.Bundle;
import android.app.Activity;
import android.text.format.Time;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView day = (TextView)findViewById(R.id.day);
    TextView month = (TextView)findViewById(R.id.month);
    TextView year = (TextView)findViewById(R.id.year);
    TextView time = (TextView)findViewById(R.id.time);


    Time today = new Time(Time.getCurrentTimezone());
    today.setToNow();

    day.setText("" + today.monthDay);             // Day of the month (0-31)
    month.setText("" + today.month);              // Month (0-11)
    year.setText("" + today.year);                // Year 
    time.setText("" + today.format("%k:%M"));  // Current time


}

}

更新:我使用这段代码得到了它:

UPDATE: I got it using this piece of code:

final Calendar calendar = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("MMM"); // 3-letter month name & 2-char day of month
    TextView datetxt = (TextView) findViewById(R.id.nameofyourtextview);
    datetxt.setText(formatter.format(calendar.getTime()));

推荐答案

试试这个

  month.setText(getMonth(today.month));    
  day.setText(getWeek(today.monthDay));  

根据月份数获取月份的方法

method to get month based on month number

public String getMonth(int month) {
    return new DateFormatSymbols().getMonths()[month];
}

根据周数获取周的方法

public String getWeek(int weekno) {
    return new DateFormatSymbols().getWeekdays()[weekno];
}

这篇关于让 TextView 将星期几显示为字母而不是整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆