# 安全卫士主页面#
###1.GridView控件
1.与ListView的使用方式差不多,也要使用数据适配器,通过设置android:numColumns控制显示几列
2.通过指定android:numColumns来控制显示多少列
###2.GridView分割线的实现方式
1.给item设置背景图片
2.给GridView和item设置不同的背景色,然后设置水平间方向间隔属性值android:horizontalSpacing和竖直方向间隔属性值android:verticalSpacing
![](http://i.imgur.com/saMRopa.png)
###3.自定义控件——开关控件
1.做自定义控件首先要分析控件都有什么功能
2.开关控件需要有一个开或者关状态,然后根据状态显示不同的图片
###4.状态选择器——selector
1.可以给View设置不同的背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按压时的背景图片-->
<item android:state_pressed="true" android:drawable="@drawable/dg_button_cancel_select"/>
<!-- 默认时的背景图片-->
<item android:drawable="@drawable/dg_button_cancel_normal"/>
</selector>
2.可以给文字设置不同的颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:color="@color/software_textColor_selected"></item>
<item android:state_selected="false" android:color="@color/software_textColor_unselected"></item>
</selector>
###5.自定义对话框
1.也是使用 AlertDialog.Builder
AlertDialog.Builder builder = new Builder(this);
2.填充出一个view
View view = View.inflate(this, R.layout.dialog_enter_pwd, null);
3.使用setView方法
builder.setView(view);
###6.给View设置图片一个简便的方法
1.设置drawableLeft、drawableTop、drawableRight、drawableBottom
###7.使用androidSDK中提供的图片
1.引用drawable的时候要在前面加上android:
<ImageView
android:src="@android:drawable/presence_online"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
###8.抽取style
1.在res/values/styles.xml文件里
2.具体实现方式
<style name="text_title">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">48dp</item>
<item name="android:background">#99CCFF</item>
<item name="android:gravity">center_vertical</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">18sp</item>
</style>
###9.给TextView设置点击事件
1.将clickable设置为true
对应代码:
1 package com.hb.mobilesafe.activities; 2 3 import android.animation.ObjectAnimator; 4 import android.annotation.SuppressLint; 5 import android.app.Activity; 6 import android.app.AlertDialog; 7 import android.app.AlertDialog.Builder; 8 import android.content.Intent; 9 import android.content.SharedPreferences; 10 import android.content.SharedPreferences.Editor; 11 import android.os.Bundle; 12 import android.text.TextUtils; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.view.ViewGroup; 16 import android.view.Window; 17 import android.widget.AdapterView; 18 import android.widget.AdapterView.OnItemClickListener; 19 import android.widget.BaseAdapter; 20 import android.widget.Button; 21 import android.widget.EditText; 22 import android.widget.GridView; 23 import android.widget.ImageView; 24 import android.widget.TextView; 25 import android.widget.Toast; 26 27 import com.hb.demo_mobilesafe.R; 28 29 public class HomeActivities extends Activity { 30 private ImageView iv_logo; 31 private GridView gridView; 32 private MyGridAdapter adapter; 33 private String [] names; 34 private String [] descs; 35 private int [] pictrues; 36 private SharedPreferences sp; 37 private AlertDialog show; 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 requestWindowFeature(Window.FEATURE_NO_TITLE); 42 setContentView(R.layout.activity_home); 43 initVeiw(); 44 initDate(); 45 gridView.setOnItemClickListener(new OnItemClickListener() { 46 47 @Override 48 public void onItemClick(AdapterView<?> parent, View view, 49 int position, long id) { 50 switch (position) { 51 //手机防盗 52 case 0: 53 //判断是进入设置密码界面还是进入输入密码界面 54 boolean tag = isSetPassWord(); 55 if(tag){ 56 //进入输入密码界面 57 mInputPassWord(); 58 }else{ 59 //进入设置面界面 60 mSetPassWord(); 61 } 62 //Toast.makeText(HomeActivities.this, "手机防盗", 0).show(); 63 break; 64 //拦截骚扰 65 case 1: 66 startActivity(new Intent(HomeActivities.this,BlackNumberActivity.class )); 67 break; 68 //软件管家 69 case 2: 70 startActivity(new Intent(HomeActivities.this,SoftwareManagerActivity.class )); 71 break; 72 //进程管理 73 case 3: 74 startActivity(new Intent(HomeActivities.this,ProcessManagerActivity.class )); 75 break; 76 //流量统计 77 case 4: 78 startActivity(new Intent(HomeActivities.this,FlowStatisticAcitivty.class )); 79 break; 80 //手机杀毒 81 case 5: 82 startActivity(new Intent(HomeActivities.this,AntiVirusActivity.class )); 83 break; 84 case 6: 85 //系统加速 86 startActivity(new Intent(HomeActivities.this,SysChacheActivity.class )); 87 break; 88 //常用工具 89 case 7: 90 startActivity(new Intent(HomeActivities.this,CommontoolsActivity.class )); 91 break; 92 93 } 94 } 95 96 97 }); 98 } 99 private void initVeiw() { 100 iv_logo=(ImageView) findViewById(R.id.iv_logo); 101 gridView=(GridView) findViewById(R.id.gridView); 102 names = new String []{"手机防盗","拦截骚扰","软件管家","进程管理", 103 "流量统计","手机杀毒","系统加速","常用工具"}; 104 descs = new String []{"远程定位手机","全面骚扰拦截","管理您的软件", 105 "管理正在运行","流量一目了然","病毒无法藏身","系统快如火箭","常用工具大全"}; 106 pictrues = new int []{R.drawable.sjfd,R.drawable.srlj,R.drawable.rjgj, 107 R.drawable.jcgl,R.drawable.lltj,R.drawable.sjsd,R.drawable.xtjs, 108 R.drawable.cygj}; 109 } 110 private void initDate() { 111 sp = getSharedPreferences("config", MODE_PRIVATE); 112 adapter = new MyGridAdapter(); 113 ObjectAnimator oa = ObjectAnimator.ofFloat(iv_logo, "rotationY", 45,90,135,180,225,270,315); 114 oa.setDuration(3000); 115 //使其重复执行动画 116 oa.setRepeatCount(ObjectAnimator.INFINITE); 117 oa.setRepeatMode(ObjectAnimator.RESTART); 118 oa.start(); 119 gridView.setAdapter(adapter); 120 } 121 //设置密码 122 private void mSetPassWord() { 123 AlertDialog.Builder builder=new Builder(HomeActivities.this); 124 View view = View.inflate(HomeActivities.this, R.layout.setpassword_item, null); 125 builder.setCancelable(false); 126 builder.setView(view); 127 show = builder.show(); 128 Button cancel=(Button) view.findViewById(R.id.bt_cancel); 129 Button confirm=(Button) view.findViewById(R.id.bt_confirm); 130 final EditText pw=(EditText) view.findViewById(R.id.et_pw); 131 final EditText repw=(EditText) view.findViewById(R.id.et_repw); 132 //确认 133 confirm.setOnClickListener(new OnClickListener() { 134 135 @Override 136 public void onClick(View v) { 137 String password =pw.getText().toString().trim(); 138 String repassword = repw.getText().toString().trim(); 139 if(TextUtils.isEmpty(password) ||TextUtils.isEmpty(repassword)){ 140 Toast.makeText(HomeActivities.this, "密码不能为空!", 0).show(); 141 return; 142 } 143 if(!password.equals(repassword)){ 144 Toast.makeText(HomeActivities.this, "亲密码错误了!", 0).show(); 145 return; 146 } 147 Editor edit = sp.edit(); 148 edit.putString("password", password); 149 edit.commit(); 150 //消失对话框 151 show.dismiss(); 152 //进入输入密码 153 mInputPassWord(); 154 } 155 }); 156 //取消 157 cancel.setOnClickListener(new OnClickListener() { 158 159 @Override 160 public void onClick(View v) { 161 show.dismiss(); 162 } 163 }); 164 165 166 167 } 168 //输入密码 169 private void mInputPassWord() { 170 171 AlertDialog.Builder builder=new Builder(HomeActivities.this); 172 View view = View.inflate(HomeActivities.this, R.layout.inputpassword_item, null); 173 builder.setCancelable(false); 174 builder.setView(view); 175 show = builder.show(); 176 Button cancel=(Button) view.findViewById(R.id.bt_cancel); 177 Button confirm=(Button) view.findViewById(R.id.bt_confirm); 178 final EditText pw=(EditText) view.findViewById(R.id.et_pw); 179 //确认 180 confirm.setOnClickListener(new OnClickListener() { 181 182 @Override 183 public void onClick(View v) { 184 String spPassword = sp.getString("password", null); 185 String password = pw.getText().toString().trim(); 186 if(TextUtils.isEmpty(password) ){ 187 Toast.makeText(HomeActivities.this, "密码不能为空!", 0).show(); 188 return; 189 } 190 if(!password.equals(spPassword)){ 191 Toast.makeText(HomeActivities.this, "亲密码错误了!", 0).show(); 192 return; 193 } 194 boolean con = sp.getBoolean("configed", false); 195 if(con){ 196 //进入防盗页面 197 lockPage(); 198 }else{ 199 //进入向导页面 200 GuidePage(); 201 } 202 //消失对话框 203 show.dismiss(); 204 205 } 206 207 208 209 210 }); 211 //取消 212 cancel.setOnClickListener(new OnClickListener() { 213 214 @Override 215 public void onClick(View v) { 216 show.dismiss(); 217 } 218 }); 219 220 221 } 222 //进入手机向导页面 223 private void GuidePage() { 224 Intent intent=new Intent(HomeActivities.this, StepOnePage_Activity.class); 225 startActivity(intent); 226 227 228 } 229 //如果sp里面的“configed”值存在,就进入手机防盗页面 230 private void lockPage() { 231 startActivity(new Intent(HomeActivities.this, LockActivity.class)); 232 233 } 234 235 class MyGridAdapter extends BaseAdapter{ 236 237 @Override 238 public int getCount() { 239 return names.length; 240 } 241 @SuppressLint("ViewHolder") @Override 242 public View getView(int position, View convertView, ViewGroup parent) { 243 View view=View.inflate(HomeActivities.this, R.layout.home_item, null); 244 ImageView iv_icon=(ImageView) view.findViewById(R.id.iv_icon); 245 TextView tv_names=(TextView) view.findViewById(R.id.tv_name); 246 TextView tv_desc=(TextView) view.findViewById(R.id.tv_desc); 247 iv_icon.setImageResource(pictrues[position]); 248 tv_names.setText(names[position]); 249 tv_desc.setText(descs[position]); 250 return view; 251 } 252 @Override 253 public Object getItem(int position) { 254 // TODO Auto-generated method stub 255 return null; 256 } 257 258 @Override 259 public long getItemId(int position) { 260 // TODO Auto-generated method stub 261 return 0; 262 } 263 264 265 266 } 267 //设置 268 public void setting(View view){ 269 Intent intent=new Intent(HomeActivities.this, SetActivities.class); 270 startActivity(intent); 271 } 272 public boolean isSetPassWord(){ 273 String password = sp.getString("password", null); 274 if(TextUtils.isEmpty(password)){ 275 return false; 276 } 277 return true; 278 } 279 }
xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <RelativeLayout 8 android:layout_width="match_parent" 9 android:layout_height="0dp" 10 android:layout_weight="1" 11 android:background="@drawable/blue_bkg" > 12 13 <ImageView 14 android:id="@+id/iv_logo" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:layout_alignParentLeft="true" 18 android:layout_centerVertical="true" 19 android:layout_margin="12dip" 20 android:src="@drawable/logo" /> 21 22 <TextView 23 android:id="@+id/tv_title" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_alignParentRight="true" 27 android:layout_marginRight="30dip" 28 android:layout_marginTop="20dip" 29 android:text="弘博手机安全卫士" 30 android:textColor="#bb00ff" 31 android:textSize="25sp" /> 32 33 <com.hb.mobilesafe.ui.TextDesc 34 android:id="@+id/tv_desc" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:layout_centerVertical="true" 38 android:layout_marginLeft="50dp" 39 android:layout_toRightOf="@id/iv_logo" 40 android:ellipsize="marquee" 41 android:focusable="true" 42 android:lines="1" 43 android:singleLine="true" 44 android:text="弘博手机安全卫士,时刻保护着你的手机一移动安全,版权所有盗版必究,http://interhb.cn" 45 android:textColor="#bbff00" /> 46 47 <ImageView 48 android:id="@+id/setting" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:layout_alignParentRight="true" 52 android:layout_alignParentTop="true" 53 android:layout_margin="10dp" 54 android:background="@drawable/setting" 55 android:onClick="setting" /> 56 </RelativeLayout> 57 58 <GridView 59 android:id="@+id/gridView" 60 android:layout_width="wrap_content" 61 android:layout_height="0dp" 62 android:layout_weight="2" 63 android:numColumns="2" /> 64 65 </LinearLayout>