仔仔手机安全卫士demo(四)

imageimage

需求:实现如上功能,用户可自由选择是否自动更新app,当选择时,提示自动更新已开启,当取消是,显示自动更新已关闭,只要点击设置自动更新字样或者选项按钮,均可选择。

接下来,使用自定义标签实现

第一步:在res/values/下创建一个attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="SettingItemView">
        <attr name="mytitle" format="string" />
        <attr name="description_on" format="string" />
        <attr name="description_off" format="string" />
    </declare-styleable>
</resources>
注意:属性的名称不能与android中已定义的名称重复,否则会抛出属性名已定义的异常
由于使用的是android studio,在前面一篇文章有提到eclipse与android studio在开发自定义标签时的区别
在使用到自定义标签的布局文件中加入该命名空间
xmlns:zaizai=http://schemas.android.com/apk/res-auto
如我再activity-setting.xml中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:zaizai="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:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="55dip"
        android:background="@android:color/holo_green_dark"
        android:gravity="center"
        android:text="设置中心"
        android:textColor="@android:color/black"
        android:textSize="22sp" />

    <com.zaizai.safty.ui.SettingItemView
        zaizai:mytitle="设置自动更新"
        zaizai:description_on="设置自动更新已经开启"
        zaizai:description_off="设置自动更新已经关闭"
        android:id="@+id/siv_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
自定义标签里的布局文件为
<?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="70dip"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="10dip"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="5dip"
        android:textColor="@android:color/black"
        android:textSize="12sp" />

    <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:layout_marginRight="10dip"
        android:clickable="false" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0.2dip"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:background="#000000"
        android:keyTextColor="@android:color/black" />

</RelativeLayout>
注意:<checkBox 一定要将其clickable置为false,使其失去被点击能力
因为CheckBox的点击事件的优先级大于TextView,如果不使其失去被点击的能力,那么当点击CheckBox的时候,自定义控件的其他组件将接收不到点击事件
自定义控件所属的类文件为
package com.zaizai.safty;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import com.zaizai.safty.ui.SettingItemView;

/**
 * 设置中心类文件
 * Created by zaizai on 2015/10/14.
 */
public class SettingActivity extends Activity {

    private SettingItemView siv_update;
    /*用来存储设置文件*/
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_setting);

        siv_update = (SettingItemView) this.findViewById(R.id.siv_update);
        preferences = getSharedPreferences("config",MODE_PRIVATE);
        boolean update_status = preferences.getBoolean("update", false);
        if (update_status) {
            /*自动升级已经开始*/
            siv_update.setChecked(true);
            /*siv_update.setDescription("自动升级已经开启");*/
        } else {
            /*自动升级已经关闭*/


            siv_update.setChecked(false);
            /*siv_update.setDescription("自动升级已经关闭");*/
        }
        siv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = preferences.edit();

                /*判断是否选中,调用自定义的函数*/
                if (siv_update.isChecked()) {
                    //自动升级已被选中,再次点击应该将其置为false
                    siv_update.setChecked(false);
                   /* siv_update.setDescription("自动升级已经关闭");*/
                    editor.putBoolean("update", false);
                } else {
                    //没有打开自动升级
                    siv_update.setChecked(true);
                    /*siv_update.setDescription("自动升级已经开启");*/
                    editor.putBoolean("update", true);
                }
                editor.commit();
            }
        });
    }
}
如上便完成了一个自定义更新控件

你可能感兴趣的:(仔仔手机安全卫士demo(四))