因为手机安全卫士每次打开都要进行检查软件版本号的工作,久而久之会浪费用户的流量。因此,我们要在设置页面中,由用户自己确认是否须要开启检查更新的操作。

效果图:

技术点:
1.自己定义组合控件
2.SharedPreferences的读写操作

自己定义组合控件
和之前自己定义风格的原因一样,都是为了降低工作量。因为该组合控件会有非常多地方要用到,因此,我们把它抽取出来,封装在一个类中。须要使用的时候直接调用就可以。

一劳永逸。

思路:

  • 创建一个布局文件。包含两个TextView,一个CheckBox如效果图的形式进行布局。
  • 创建一个类,继承自RelativeLayout(假设布局是採用线性布局,则继承自LinearLayout)
  • 自己定义组合控件的属性

1.布局文件:
setting_item_layout.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="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="是否自己主动更新"
        android:textSize="30dp"
        android:textColor="#000"
        android:id="@+id/tv_setting_item_title"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="已经开启自己主动更新"
        android:textSize="24dp"
        android:id="@+id/tv_setting_item_content"
        android:layout_below="@id/tv_setting_item_title"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:id="@+id/cb_setting_item"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="0.3dp"
        android:layout_marginTop="10dp"
        android:background="#33000000"
        android:layout_marginBottom="10dp"
        android:layout_below="@id/tv_setting_item_content"
        />
</RelativeLayout>

2.自己定义控件属性:
(1)创建一个自己定义的命名空间:
xmlns:mobilesafe=”http://schemas.android.com/apk/res/包名”
(2)在values/styles 文件里加入例如以下代码:

 <declare-styleable name="SettingItemView">
        <attr name="content_on" format="string" />
        <attr name="content_off" format="string" />
        <attr name="title" format="string" />
    </declare-styleable>

(3)在代码中,做例如以下操作:

  • 载入布局
    View.inflate(Context ResourceID ViewGroup)
  • 初始化控件(findViewById)
  • 获取布局文件上的属性值
title = attrs.getAttributeValue(NAMESPACE,"title");
content_on = attrs.getAttributeValue(NAMESPACE,"content_on");
content_off = attrs.getAttributeValue(NAMESPACE,"content_off");

`
     - 依据自己的实际开发需求,设置一些属性:
 public void setChecked(boolean isChecked)
    {
        if(isChecked)
        {
            cb.setChecked(true);
            setContent(content_on);
        }
        else
        {
            cb.setChecked(false);
            setContent(content_off);
        }
    }

    public boolean isChecked()
    {
        return cb.isChecked();
    }

    private void setContent(String content)
    {
        tvContent.setText(content);
    }
在SettingActivity的布局文件里。加入例如以下控件:
<com.vincentliong.mobilesafe0722.ui.SettingItemView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    mobilesafe:title="是否开启自己主动更新"
    mobilesafe:content_on="自己主动更新已开启"
    mobilesafe:content_off="自己主动更新已关闭"
    android:id="@+id/settingitem_setting_update"
    />

然后。在SettingActivity中,创建自己定义控件对象,并进行对应的操作

if(isChecked)
        {
            mSivUpdate.setChecked(true);
        }
        else
        {
            mSivUpdate.setChecked(false);
        }
        mSivUpdate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                if(mSivUpdate.isChecked())
                {
                    mSivUpdate.setChecked(false);
                    editor.putBoolean("autoUpdate",false);
                }
                else
                {
                    mSivUpdate.setChecked(true);
                    editor.putBoolean("autoUpdate",true);
                }
                editor.commit();
            }
        });

最后,在SplashActivity中加入推断操作,当SharedPreferences中的antoUpdate值为true时再运行自己主动更新操作,否则,直接进入主页面。
搞定! 

阅读全文
版权声明:刚出锅的原创内容,希望对你有帮助~ 举报
  • 本文已收录于下面专栏:
0条评论