一只正在一步一步往上爬的Android小白

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

效果图:

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

自定义组合控件 和之前自定义风格的原因一样,都是为了减少工作量。由于该组合控件会有很多地方要用到,因此,我们把它抽取出来,,封装在一个类中,需要使用的时候直接调用即可。一劳永逸!

思路:

1.布局文件: setting_item_layout.xml

==”wrap_content”><TextView===”#000″android:id=”@+id/tv_setting_item_title”/><TextView===”@+id/tv_setting_item_content”android:layout_below=”@id/tv_setting_item_title”/><CheckBox===”@+id/cb_setting_item”/><View==”10dp”android:layout_below=”@id/tv_setting_item_content”/></RelativeLayout>

2.自定义控件属性: (1)创建一个自定义的命名空间: xmlns:mobilesafe=”包名” (2)在values/styles 文件中加入如下代码:

===>

(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”);`- 根据自己的实际开发需求,设置一些属性: (boolean isChecked){if(isChecked){cb.setChecked(true);setContent(content_on);}else{cb.setChecked(false);setContent(content_off);}}(){return cb.isChecked();}(String content){tvContent.setText(content);}在SettingActivity的布局文件中,添加如下控件:<com.vincentliong.mobilesafe0722.ui.SettingItemViewandroid: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() {(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时再执行自动更新操作,否则,直接进入主页面。 搞定! 

每天告诉自己一次,『我真的很不错』

一只正在一步一步往上爬的Android小白

相关文章:

你感兴趣的文章:

标签云: