首先实现九大模块中“设置中心”的“自动更新”选项,默认该选项是开启的:打勾并且字体白色。
关闭状态:去勾并且字体设置为灰色。
效果图:
在主界面中为item添加点击事件,当点击“设置中心”item时打开对应的Activity:
package com.example.mobilesafe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
/**
* Created by sing on 13-12-24.
* desc:
*/
public class MainActivity extends Activity {
//activity_main中的gridview控件
private GridView gv_main;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv_main = (GridView)findViewById(R.id.gv_main);
//为gv_main对象设置适配器,该适配器为每个item填充对应的数据
gv_main.setAdapter(new MainAdapter(this));
//为gv_main设置点击item的处理事件
gv_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
/**
* item点击事件
* @param adapterView
* @param view
* @param i
* @param l
*/
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 8: //设置中心
Intent intent = new Intent(MainActivity.this, SettingCenterActivity.class);
startActivity(intent);
break;
}
}
});
}
}
设计“设置中心”的界面:
效果图中的checkbox勾选框在文本右侧显示,实际上是一个TextView和一个不带文本的CheckBox组合而成,把它们放置于一个RelativeLayout里,并设置TextView靠父控件左对齐,CheckBox靠父控件右对齐。
整体布局为LinearLayout,从上到下依次为TextView显示“设置中心”,一个View显示实线分割条,一个RelativeLayout显示TextView和CheckBox,底部一个View显示虚线分割条。对应的layout.xml为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置中心"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textStyle="bold" />
<View
android:layout_height="1dip"
android:layout_width="fill_parent"
android:layout_marginTop="5dip"
android:background="@drawable/devide_line" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动更新设置"
android:id="@+id/tv_setting_autoupdate_text"
android:layout_gravity="center_horizontal|left"
android:textSize="30sp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cb_setting_autoupdate"
android:checked="true"
android:singleLine="false"
android:layout_gravity="right"
android:textSize="30sp"
android:layout_alignBottom="@+id/tv_setting_autoupdate_text"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="true" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动更新已开启"
android:id="@+id/tv_setting_autoupdate_status"
android:textSize="20sp" />
<View
android:layout_height="1dip"
android:layout_width="fill_parent"
android:layout_marginTop="5dip"
android:background="@drawable/listview_devider" />
</LinearLayout>
“设置中心”在启动时,获取配置信息中的“自动更新”设置状态,根据状态勾选checkbox以及设置状态文本和颜色。
package com.example.mobilesafe;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
/**
* Created by sing on 13-12-24.
* desc:
*/
public class SettingCenterActivity extends Activity {
//设置自动更新的checkbox
private CheckBox cb_setting_autoupdate;
//显示自动更新开启状态的文本框
private TextView tv_setting_autoupdate_status;
//保存配置
private SharedPreferences sp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settingcenter);
tv_setting_autoupdate_status = (TextView)findViewById(R.id.tv_setting_autoupdate_status);
cb_setting_autoupdate = (CheckBox)findViewById(R.id.cb_setting_autoupdate);
//加载上次设置的状态
sp = getSharedPreferences("config", MODE_PRIVATE);
boolean autoupdate = sp.getBoolean("autoupdate", true);
cb_setting_autoupdate.setChecked(autoupdate);
tv_setting_autoupdate_status.setText(autoupdate?"自动更新已经开启":"自动更新已经关闭");
tv_setting_autoupdate_status.setTextColor(autoupdate? Color.WHITE:Color.GRAY);
//为checkbox编写响应事件
cb_setting_autoupdate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("autoupdate", b);
editor.commit();
tv_setting_autoupdate_status.setText(b?"自动更新已经开启":"自动更新已经关闭");
tv_setting_autoupdate_status.setTextColor(b? Color.WHITE:Color.GRAY);
}
});
}
}