題目:
在Android Studio中輸入《Android權(quán)威指南》“第7章 UI fragment與fragment管理器”中的項目代碼并運行之。即設(shè)計一個名為CrimeFragment的UI fragment來管理圖1所示的用戶界面拦宣,再設(shè)計一個名為CrimeActivity的activity來托管CrimeFragment實例截粗。
[注意]
要求將按鈕中顯示的日期格式調(diào)整為人們習(xí)慣看到的日期格式,如“May 22, 2018”等日期格式鸵隧。
解答:
1.在fragment_crime.xml下添加新組件
<CheckBox android:id="@+id/crime_solved"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="@string/crime_solved_label"
/>
2.在CrimeFragment.java設(shè)置Button上的文字顯示在CrimeFragment.java
//默認(rèn)日期格式
mDateButton = (Button)v.findViewById(R.id.crime_date);
mDateButton.setText(mCrime.getDate().toString());
mDateButton.setEnabled(false);
在這里不用按鈕可以確保它不響應(yīng)用戶的單擊事件绸罗。禁用后,按鈕的外觀樣式也會發(fā)生改變(變?yōu)?灰色)豆瘫,表明它已處于禁用狀態(tài)
3.修改button日期格式
將2的代碼替換成如下代碼
//修改后的日期格式
mDateButton = v.findViewById(R.id.crime_date);
String dateTime = SimpleDateFormat.getDateTimeInstance().format(mCrime.getDate());
mDateButton.setText(dateTime);