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);
        }
    }
}
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5095281.html,如需转载请自行联系原作者

相关文章
|
7月前
|
JSON API Android开发
Android bundetool 详解
Android bundetool 详解
|
Java 测试技术 Android开发
Android LayoutAnimation不生效
Android LayoutAnimation不生效
105 0
|
Android开发
Android | 理解 ViewRootImpl
Android | 理解 ViewRootImpl
Android | 理解 ViewRootImpl
|
XML SQL IDE
Android Studio快捷键以及使用技巧
Android Studio快捷键以及使用技巧
304 0
|
IDE API 开发工具
android下的SuppressLint与TargetApi
android下的SuppressLint与TargetApi
94 0
|
Android开发
Android书籍分享
在这里将我收集的一些书籍,与大家分享。 1、介绍Android 与GPhone的书籍 Introduction To Android (下载)Google官方文档,简要的介绍了Android的各个方面,值得大家看下 GPhone说明书 (下载)是每个开发人员必须阅读的,关于GPhone的使用、功能...
747 0
|
Android开发
android 记一次解决键盘遮挡问题
文章链接:https://mp.weixin.qq.com/s/1gkMtLu0BTXOUOj6isDjUw 日常android开发过程中,会遇到编辑框输入内容弹出软键盘,往往会出现键盘遮挡内容,或者出现页面整体上移的,或多或少在体验上都不是很优雅,今天提供个方法是自行控制页面上移距离,竟可能让页面呈现给用户友好点。
2430 0
|
Web App开发 API Android开发
Android 优秀文章
https://academy.realm.io/cn/posts/droidcon-farber-improving-android-app-performance/ https://academy.
1165 0
|
Android开发 API
Android BaseRecyclerViewAdapterHelper使用
BaseRecyclerViewAdapterHelper项目下载 1 . 使用 // 基类 具体实现类 布局 数据源 ...
2327 0