String[] datesReason = {
"New Years Day",
"Martin Luther King, Jr.'s Birthday",
"Asian Lunar New Year"
};
public String[] suspendedDates = {
"Friday, January 1",
"Monday, January 18",
"Saturday, March 5"
};
DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);
Spannable WordToSpan = new SpannableString("Today is: ");
WordToSpan.setSpan(new ForegroundColorSpan(R.color.dateholiday), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvTD.setText(WordToSpan);
Spannable WordToSpan1 = new SpannableString("" + df.format(new Date()) + "");
WordToSpan1.setSpan(new ForegroundColorSpan(Color.parseColor("#4787ED")), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvTD.append(WordToSpan1);
上面的代碼在我從PC上調試到Android模擬器時顯示Today is: Saturday, March 5
。當我使用Android設備(運行棒棒糖,Jellybean等)進行調試時,它顯示Today is: S, March 5
爲什麼模擬器和手機中的顯示不一樣
我也使用上面的來比較日期的日期做其他功能,並且我有以下內容,它比較日期今天:
SetRows myImage = data.get(position); //SetRows is what I am using to populate a ListView with texts/images
int inReason = myImage.name.indexOf(",");
String strR = myImage.name.substring(0, inReason);
Spannable WordToSpan = new SpannableString(strR); //e.g. strR = "Saturday"
WordToSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#4787ED")), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
String strRNext = myImage.name.substring(inReason, myImage.name.length());
Spannable WordToSpan1 = new SpannableString(strRNext); //e.g. strRNext = "March 5"
WordToSpan1.setSpan(new ForegroundColorSpan(R.color.dateholiday), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))) { //if Today is Saturday, March 5, the set the background of the list item...
Toast.makeText(getContext(), String.valueOf(myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))), 2000).show();
row.setBackgroundColor(Color.parseColor("#F0DEEDF3"));
}
else { //otherwise use the default background...
row.setBackgroundColor(Color.parseColor("#CCF5F5F5"));
}
在模擬器中正確的列表項將獲得的背景,但在手機上,它並沒有因爲它顯示的S
代替Saturday
是否知道這是爲什麼發生的事情,以及如何能我解決它。
「在Android 4.3(Jelly Bean MR2,API level 18)中引入了用於字段的最短非數字表示的五個統計模式(例如」MMMMM「)」(來自[文檔](http: //developer.android.com/reference/java/text/SimpleDateFormat.html))。如果你的'minSdkVersion'小於18,那麼無論如何你需要制定一些其他解決方案。 – CommonsWare
我目前有'11'作爲minSDK版本。這就是爲什麼在我的棒棒糖手機中日期是以'S'代替'Satuday'出現的嗎?模擬器正在運行4.1.2,也許這就是它在模擬器中工作的原因。 – Si8