*Calendar、DataPickerDialog
SimpleDateFormat sdf;
java.util.Date dt;
Calendar c = Calendar.getInstance();
try {
//設置日期格式爲"yyyy-MM-dd"
sdf = new SimpleDateFormat("yyyy-MM-dd");
//將string轉化爲SimpleDateFormat
dt = sdf.parse(userUtil.getBirth());
//對calendar設置日期格式
c.setTime(dt);
} catch (ParseException e) {
e.printStackTrace();
}
//設置DatePickerDialog
DatePickerDialog dpd = DatePickerDialog.newInstance(
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
int month = monthOfYear + 1;
String formatMonth;
String formatDay;
if (month < 10) {
formatMonth = "-0" + month;
} else {
formatMonth = "-" + month;
}
if (dayOfMonth < 10) {
formatDay = "-0" + dayOfMonth;
} else {
formatDay = "-" + dayOfMonth;
}
birth = year + formatMonth + formatDay;
}
},
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH)
);
//設置DataPickerDialog可用的,最大日期爲現在的日期
dpd.setMaxDate(Calendar.getInstance());
dpd.show(getFragmentManager(), "DatePickerDialog");
*PreferenceActivity
1、佈局文件setting_pref.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:key="universal"
android:title="通用">
<Preference
android:key="account_manager"
android:title="@string/account_manager">
<!--隱式啓動SetAccountActivity-->
<intent android:action="com.setting.SetAccountActivity" />
</Preference>
<Preference
android:title="@string/cache_manager">
<!--隱式啓動SetCacheActivity-->
<intent android:action="com.setting.SetCacheActivity" />
</Preference>
</PreferenceCategory>
<PreferenceCategory
android:key="others"
android:title="其它">
<Preference
android:key="upgrade"
android:title="@string/to_upgrade" />
</PreferenceCategory>
</PreferenceScreen>
2、SettingsActivity.java
public class SettingsActivity extends AppCompatPreferenceActivity {
private static final String TAG = SettingsActivity.class.getSimpleName();
/**
*do some nesseary declaration
*...
*Preference account_manager;
*SwitchPreference not_wifi_watch;
*boolean not_wifi_watch_flag;
*Preference upgrade
*...
*/
/**
* Helper method to determine if the device has an extra-large screen. For
* example, 10" tablets are extra-large.
*/
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//添入佈局文件
addPreferencesFromResource(R.xml.setting_pref) ;
PreferenceManager.setDefaultValues(this, R.xml.setting_pref, false);
mContext = this;
initView();
initData();
}
private void initView() {
//是Toolbar作用爲ActionBar
setupActionBar();
//由於AppCompatPreferenceActivity,使顯示返回鍵
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
account_manager = (Preference) findPreference("account_manager");
not_wifi_watch = (SwitchPreference) findPreference("not_wifi_watch");
upgrade = (Preference) findPreference("upgrade");
if (QingjiaoApplication.getPreference("wifi_watch").equals("close")) {
not_wifi_watch.setChecked(false);
not_wifi_watch_flag = false;
} else {
not_wifi_watch_flag = true;
not_wifi_watch.setChecked(true);
}
}
private void initData() {
not_wifi_watch.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (!not_wifi_watch_flag) {
not_wifi_watch.setChecked(true);
QingjiaoApplication.savePreference(QingjiaoApplication.NOT_WIFI_WATCH, "open");
not_wifi_watch_flag = true;
} else {
not_wifi_watch.setChecked(false);
QingjiaoApplication.savePreference(QingjiaoApplication.NOT_WIFI_WATCH, "close");
not_wifi_watch_flag = false;
}
return true;
}
});
upgrade.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (!Util.isGoOn("upgrade")) {
return true;
}
if (!Util.hasInternet()) {
Util.showTips(R.string.none_network);
} else {
toRequestUpgradeOrNot();
}
return true;
}
});
}
private void toRequestUpgradeOrNot() {
IBeanLoader iBeanLoader = new BeanLoaderImpl(this);
iBeanLoader.loadHttp(new UpgradeBean());
iBeanLoader.setCallback(new IBeanLoader.ILoadCallback<UpgradeResult>() {
@Override
public void onCacheComplete(UpgradeResult result) {
}
@Override
public void onHttpComplete(int resultCode, final UpgradeResult result) {
if (this == null || resultCode != IBeanLoader.LOAD_SUCCESS || result == null || result.result == null) {
processDialog.dismiss();
Util.showTips("獲取版本信息失敗,請稍後重試");
return;
}
Logger.d(TAG, " toRequestUpgradeOrNot()---->Result");
if (result.code == 0) {
if (TextUtils.isEmpty(result.result.up_to_version)) {
Util.showTips(R.string.no_to_download);
} else {
Intent intent = new Intent();
//啓動瀏覽器進行下載
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(result.result.up_download);
//傳url
intent.setData(content_url);
startActivity(intent);
}
}
}
@Override
public void onContentChange() {
}
});
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this);
}
@Override
protected void onResume() {
super.onResume();
if (UserUtil.getInstance().isLoginTag()) {
//有效登錄則"account_manager"顯示
((PreferenceCategory) findPreference("universal")).addPreference(account_manager);
} else {
//無有效登錄則"account_manager"不顯示
((PreferenceCategory) findPreference("universal")).removePreference(account_manager);
}
}
/**
* 添加返回鍵功能
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
*CoordinatorLayout
1)使用ToolBar作爲actionbar這篇文章的指南。
2)CoordinatorLayout作爲主佈局容器。
3)AppBarLayout來讓Toolbar響應滾動事件。
4)在RecyclerView或者任意支持嵌套滾動的view上添加app:layout_behavior.它和AppBarLayout。ScrollingViewBehavior相匹配。
5)CollapsingToolbarLayout中定義在AppBarLayout裏面:製造摺疊效果
定義的view只要設置了app:layout_scrollFlags屬性
可以使用的其他flag有:
enterAlways: 一旦向上滾動這個view就可見。
enterAlwaysCollapsed: 顧名思義,這個flag定義的是何時進入(已經消失之後何時再次顯示)。假設你定義了一個最小高度(minHeight)同時enterAlways也定義了,那麼 view將在到達這個最小高度的時候開始顯示,並且從這個時候開始慢慢展開,當滾動到頂部的時候展開完。
exitUntilCollapsed: 同樣顧名思義,這個flag時定義何時退出,當你定義了一個minHeight,這個view將在滾動到達這個最小高度的時候消失。
6)CollapseMode :子視圖的摺疊模式,有兩種
“pin”:固定模式,在摺疊的時候最後固定在頂端;
“parallax”:視差模式,在摺疊的時候會有個視差摺疊的效果。
2)CoordinatorLayout作爲主佈局容器。
3)AppBarLayout來讓Toolbar響應滾動事件。
4)在RecyclerView或者任意支持嵌套滾動的view上添加app:layout_behavior.它和AppBarLayout。ScrollingViewBehavior相匹配。
5)CollapsingToolbarLayout中定義在AppBarLayout裏面:製造摺疊效果
定義的view只要設置了app:layout_scrollFlags屬性
可以使用的其他flag有:
enterAlways: 一旦向上滾動這個view就可見。
enterAlwaysCollapsed: 顧名思義,這個flag定義的是何時進入(已經消失之後何時再次顯示)。假設你定義了一個最小高度(minHeight)同時enterAlways也定義了,那麼 view將在到達這個最小高度的時候開始顯示,並且從這個時候開始慢慢展開,當滾動到頂部的時候展開完。
exitUntilCollapsed: 同樣顧名思義,這個flag時定義何時退出,當你定義了一個minHeight,這個view將在滾動到達這個最小高度的時候消失。
6)CollapseMode :子視圖的摺疊模式,有兩種
“pin”:固定模式,在摺疊的時候最後固定在頂端;
“parallax”:視差模式,在摺疊的時候會有個視差摺疊的效果。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.youtu.testcoordinatelayout.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:id="@+id/personal_detail_head"
android:layout_width="match_parent"
android:layout_height="175dp"
android:background="#eded75"></RelativeLayout>
<!--對應6)-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/personal_detail_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/personal_detail_head"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>