zoukankan      html  css  js  c++  java
  • android131 360 02 设置中心

    // 判断是否需要自动更新
            boolean autoUpdate = mPref.getBoolean("auto_update", true);
            if (autoUpdate) {
                checkVerson();
            } else {
                mHandler.sendEmptyMessageDelayed(CODE_ENTER_HOME, 2000);// 延时2秒后发送消息
            }
            // 渐变的动画效果
            AlphaAnimation anim = new AlphaAnimation(0.3f, 1f);//透明度从0.3到1
            anim.setDuration(2000);//延迟时间是2秒
            rlRoot.startAnimation(anim);//rlRoot是闪屏页xml的根布局

    SettingActivity.java

    package com.itheima52.mobilesafe.activity;
    
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    
    import com.itheima52.mobilesafe.R;
    import com.itheima52.mobilesafe.view.SettingItemView;
    
    /**
     * 设置中心
     */
    public class SettingActivity extends Activity {
    
        private SettingItemView sivUpdate;// 设置升级
        private SharedPreferences mPref;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_setting);
    
            mPref = getSharedPreferences("config", MODE_PRIVATE);
    
            sivUpdate = (SettingItemView) findViewById(R.id.siv_update);
            // sivUpdate.setTitle("自动更新设置");
    
            boolean autoUpdate = mPref.getBoolean("auto_update", true);//是否自动更新,true是默认值。
    
            if (autoUpdate) {
                // sivUpdate.setDesc("自动更新已开启");
                sivUpdate.setChecked(true);
            } else {
                // sivUpdate.setDesc("自动更新已关闭");
                sivUpdate.setChecked(false);
            }
    
            sivUpdate.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // 判断当前的勾选状态
                    if (sivUpdate.isChecked()) {
                        // 设置不勾选
                        sivUpdate.setChecked(false);
                        // sivUpdate.setDesc("自动更新已关闭");
                        // 更新是否自动更新
                        mPref.edit().putBoolean("auto_update", false).commit();
                    } else {
                        sivUpdate.setChecked(true);
                        // sivUpdate.setDesc("自动更新已开启");
                        // 更新是否自动更新
                        mPref.edit().putBoolean("auto_update", true).commit();
                    }
                }
            });
        }
    }

    activity_setting.xml

    <!-- 
        xmlns:android="http://schemas.android.com/apk/res/android"中的android是命名空间,
        xmlns:itheima="http://schemas.android.com/apk/res/com.itheima52.mobilesafe"中的itheima是命名空间,com.itheima52.mobilesafe是清单文件中的包名。
    -->
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:itheima="http://schemas.android.com/apk/res/com.itheima52.mobilesafe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            style="@style/TitleStyle"
            android:text="设置中心" />
        <!--style.xml
            <style name="TitleStyle">
                <item name="android:layout_width">match_parent</item>
                <item name="android:layout_height">50dp</item>
                <item name="android:background">#8866ff00</item>
                <item name="android:gravity">center</item>
                <item name="android:textColor">@color/black</item>
                <item name="android:textSize">22sp</item>
            </style> 
        -->
        
        <!-- 加载这个控件的时候会去调用SettingItemView的构造函数,
        调用构造函数的时候会去执行initView()方法加载R.layout.view_setting_item布局。 -->
        <com.itheima52.mobilesafe.view.SettingItemView
            android:id="@+id/siv_update"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            itheima:desc_off="自动更新已关闭"
            itheima:desc_on="自动更新已开启"
            itheima:title="自动更新设置" />
        
    <!--自定义属性,values文件夹下attrs.xml,SettingItemView相当于是一个TextView,下面是TextView的属性。
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="SettingItemView">
            <attr name="title" format="string" />
            <attr name="desc_on" format="string" />
            <attr name="desc_off" format="string" />
        </declare-styleable>
    </resources> -->
    
    </LinearLayout>

    自定义控件SettingItemView.java

    package com.itheima52.mobilesafe.view;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    import com.itheima52.mobilesafe.R;
    
    /**
     * 设置中心的自定义组合控件
     */
    public class SettingItemView extends RelativeLayout {//RelativeLayout是一个ViewGroup也就是一个View的容器。
        //NAMESPACE是xmlns:itheima="http://schemas.android.com/apk/res/com.itheima52.mobilesafe"中itheima的位置。
        private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.itheima52.mobilesafe";
        private TextView tvTitle;
        private TextView tvDesc;
        private CheckBox cbStatus;
        private String mTitle;
        private String mDescOn;
        private String mDescOff;
        //xml布局解析成java对象的时候有style走这个方法
        public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            initView();
        }
        //xml布局解析成java对象的时候有属性走这个方法
        public SettingItemView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mTitle = attrs.getAttributeValue(NAMESPACE, "title");// 根据属性名称,获取属性的值
            mDescOn = attrs.getAttributeValue(NAMESPACE, "desc_on");
            mDescOff = attrs.getAttributeValue(NAMESPACE, "desc_off");
            initView();
             
             int attributeCount = attrs.getAttributeCount();
             for (int i = 0; i < attributeCount; i++) {
                 String attributeName = attrs.getAttributeName(i);
                 String attributeValue = attrs.getAttributeValue(i);
                 System.out.println(attributeName + "=" + attributeValue);
             }
        }
        //不通过xml布局用代码new走这个方法
        public SettingItemView(Context context) {
            super(context);
            initView();
        }
    
        /**
         * 初始化布局
         */
        private void initView() {
            // 将自定义好的布局文件设置给当前的SettingItemView,this将成为view_setting_item的父容器,this是一个ViewGroup是一个view容器可以拥有子布局。
            View.inflate(getContext(), R.layout.view_setting_item, this);//View里面用getContext()拿到context对象。
            //view_setting_item.xml
            /*<?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="70dp"        整个RelativeLayout的高度
                android:padding="5dp" >
                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/black"
                    android:textSize="22sp" />
                <TextView
                    android:id="@+id/tv_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/tv_title"
                    android:layout_marginTop="3dp"
                    android:textColor="#a000"
                    android:textSize="18sp" />
                <CheckBox
                    android:id="@+id/cb_status"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:clickable="false"                不能点击
                    android:focusable="false"                不能获取焦点
                    android:focusableInTouchMode="false" />     触摸状态能不能获取焦点
                <View
                    android:layout_width="match_parent"
                    android:layout_height="0.2dp"
                    android:layout_alignParentBottom="true"
                    android:background="#a000" />
            </RelativeLayout>*/
            tvTitle = (TextView) findViewById(R.id.tv_title);
            tvDesc = (TextView) findViewById(R.id.tv_desc);
            cbStatus = (CheckBox) findViewById(R.id.cb_status);
    
            setTitle(mTitle);// 设置标题
        }
    
        public void setTitle(String title) {
            tvTitle.setText(title);
        }
    
        public void setDesc(String desc) {
            tvDesc.setText(desc);
        }
    
        /**
         * 返回勾选状态
         */
        public boolean isChecked() {
            return cbStatus.isChecked();
        }
    
        public void setChecked(boolean check) {
            cbStatus.setChecked(check);
    
            // 根据选择的状态,更新文本描述
            if (check) {
                setDesc(mDescOn);
            } else {
                setDesc(mDescOff);
            }
        }
    }
  • 相关阅读:
    我的友情链接
    我的友情链接
    BuChain 介绍:视屏讲解
    2019年5月数据库流行度排行:老骥伏枥与青壮图强
    五一4天就背这些Python面试题了,Python面试题No12
    钱包:BUMO 小布口袋 APP 用户手册
    工具箱:BUMO 工具应用场景示例
    工具箱:BUMO 密钥对生成器用户手册
    开发指南:BUMO 智能合约 Java 开发指南
    钱包:BOMO 轻钱包用户手册
  • 原文地址:https://www.cnblogs.com/yaowen/p/5095281.html
Copyright © 2011-2022 走看看