Android UI(5)-- Calendar、DataPickerDialog、PreferenceActivity、CoordinatorLayout

杭英杰
2023-12-01

目录

*Calendar、DataPickerDialog

*PreferenceActivity

*CoordinatorLayout


*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”:视差模式,在折叠的时候会有个视差折叠的效果。
 

<?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>

 

 类似资料: