activity_home.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text1" style="@style/TitleStyle" android:text="功能列表" />
//自定义TextView 强制有焦点 <com.mxn.mobilesafe.view.FocusedTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="22sp" android:text="新消息: 今日。。。。。" android:singleLine="true" //展示一行 android:ellipsize="marquee"//走马灯。。start省略前面的内容。。 android:layout_marginTop="10dp" android:layout_marginBottom="10dp" />
//给TextView加上这两行代码,也可以实现走马灯 <!-- android:focusableInTouchMode="true" -->
<!-- android:focusable="true" -->
<GridView android:id="@+id/gv_home" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:verticalSpacing="30dp" android:numColumns="3" >//展示3列 </GridView> </LinearLayout>
FocusedTextView.java
//自定义TextView,获取焦点的TextView public class FocusedTextView extends TextView{ //有style样式的会走此方法 public FocusedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } //有属性时的会走此方法 public FocusedTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } //用代码new对象时走此方法 public FocusedTextView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override public boolean isFocused() { // 表示是否获取焦点,走马灯要运行,首先调用此函数,判断是否有焦点,是true,走马灯才会有效果。强制返回true return true; } }
home_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/iv_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/home_apps" /> <TextView android:id="@+id/tv_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:textColor="@color/black" android:textSize="20sp" /> </LinearLayout>
style.xml
<resources> <style name="TitleStyle"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">100dp</item> <item name="android:background">#8866ff00</item> <item name="android:textColor">@color/black</item> <item name="android:gravity">center</item> <item name="android:textSize">22sp</item> </style> </resources>
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="black">#000</color> </resources>
HomeActivity.java
public class HomeActivity extends Activity { private GridView gvHome; private String[] mItems = new String[] { "手机防盗", "通讯卫士", "软件管理", "进程管理", "流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心" }; private int[] mPics = new int[] { R.drawable.home_safe, R.drawable.home_callmsgsafe, R.drawable.home_apps, R.drawable.home_taskmanager, R.drawable.home_netmanager, R.drawable.home_trojan, R.drawable.home_sysoptimize, R.drawable.home_tools, R.drawable.home_settings }; private SharedPreferences mPref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); mPref = getSharedPreferences("config", MODE_PRIVATE); gvHome = (GridView) findViewById(R.id.gv_home); gvHome.setAdapter(new HomeAdapter()); // 设置每一项的监听点击事件 gvHome.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: // 手机防盗 showPasswordDialog(); break; case 8: // 设置中心 startActivity(new Intent(HomeActivity.this, SettingActivity.class)); break; case 7: //高级工具 startActivity(new Intent(HomeActivity.this, AtoolsActivity.class)); break; case 1: //通讯卫士,黑名单 startActivity(new Intent(HomeActivity.this,CallSafeActivity.class)); break; case 2: //软件管理 startActivity(new Intent(HomeActivity.this,AppManagerActivity.class)); break; case 3: //进程管理 startActivity(new Intent(HomeActivity.this,TaskManagerActivity.class)); break; case 5: //病毒查杀 startActivity(new Intent(HomeActivity.this,AntivirusActivity.class)); break; case 6: //缓存清理 startActivity(new Intent(HomeActivity.this,CleanCacheActivity.class)); break; case 4: //流量管理 startActivity(new Intent(HomeActivity.this,TrafficManagerActivity.class)); default: break; } } }); } /** * 显示密码弹窗 */ protected void showPasswordDialog() { // 判断是否设置密码 String savedPassword = mPref.getString("password", null); if (!TextUtils.isEmpty(savedPassword)) { // 输入密码弹窗 showPasswordInputDialog(); } else { // 如果没有设置过, 弹出设置密码的弹窗 showPasswordSetDailog(); } } /** * 输入密码弹窗 */ private void showPasswordInputDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dialog_input_password, null); // dialog.setView(view);// 将自定义的布局文件设置给dialog dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题 final EditText etPassword = (EditText) view.findViewById(R.id.et_password); Button btnOK = (Button) view.findViewById(R.id.btn_ok); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); btnOK.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String password = etPassword.getText().toString(); if (!TextUtils.isEmpty(password)) { String savedPassword = mPref.getString("password", null); if (MD5Utils.encode(password).equals(savedPassword)) { // Toast.makeText(HomeActivity.this, "登录成功!", // Toast.LENGTH_SHORT).show(); dialog.dismiss(); // 跳转到手机防盗页 startActivity(new Intent(HomeActivity.this, LostFindActivity.class)); } else { Toast.makeText(HomeActivity.this, "密码错误!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(HomeActivity.this, "输入框内容不能为空!", Toast.LENGTH_SHORT).show(); } } }); btnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss();// 隐藏dialog } }); dialog.show(); } /** * 设置密码的弹窗 */ private void showPasswordSetDailog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dialog_set_password, null); // dialog.setView(view);// 将自定义的布局文件设置给dialog dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题 final EditText etPassword = (EditText) view.findViewById(R.id.et_password); final EditText etPasswordConfirm = (EditText) view.findViewById(R.id.et_confirm); Button btnOK = (Button) view.findViewById(R.id.btn_ok); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); btnOK.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String password = etPassword.getText().toString(); String passwordConfirm = etPasswordConfirm.getText().toString(); // password!=null && !password.equals("") if (!TextUtils.isEmpty(password) && !passwordConfirm.isEmpty()) { if (password.equals(passwordConfirm)) { // Toast.makeText(HomeActivity.this, "登录成功!", // Toast.LENGTH_SHORT).show(); // 将密码保存起来 mPref.edit().putString("password", MD5Utils.encode(password)).commit(); dialog.dismiss(); // 跳转到手机防盗页 startActivity(new Intent(HomeActivity.this, LostFindActivity.class)); } else { Toast.makeText(HomeActivity.this, "两次密码不一致!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(HomeActivity.this, "输入框内容不能为空!", Toast.LENGTH_SHORT).show(); } } }); btnCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss();// 隐藏dialog } }); dialog.show(); }
class HomeAdapter extends BaseAdapter { @Override public int getCount() { return mItems.length;//展示多少数据 } @Override public Object getItem(int position) { return mItems[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) {
//加载布局 View view = View.inflate(HomeActivity.this, R.layout.home_list_item, null);
//获取到每一项的具体内容 ImageView ivItem = (ImageView) view.findViewById(R.id.iv_item); TextView tvItem = (TextView) view.findViewById(R.id.tv_item); //给girdview的每一项具体内容设置内容 tvItem.setText(mItems[position]); ivItem.setImageResource(mPics[position]);
//返回当前的position项 return view; } } }
dialog_set_password.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:background="#66ff6600" android:padding="20dp" android:textSize="20sp" android:text="设置密码" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="请输入密码" android:inputType="textPassword" > <requestFocus /> </EditText> <EditText android:id="@+id/et_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="请再次输入密码" android:inputType="textPassword" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_ok" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/btn_cancel" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout>
dialog_input_password.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:background="#66ff6600" android:padding="20dp" android:textSize="20sp" android:text="输入密码" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="请输入密码" android:inputType="textPassword" > <requestFocus /> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_ok" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/btn_cancel" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout>
MD5Utils.java
public class MD5Utils { public static String encode(String password){ StringBuffer sb = new StringBuffer(); MessageDigest instance; try { instance = MessageDigest.getInstance("MD5"); byte[] digest = instance.digest(password.getBytes());//对字符串加密,返回加密后的字节数组 for(byte b:digest){ int i = b&0xff;//获取字节的低八位有效值 String hexString = Integer.toHexString(i);//将一个整数转化为16进制 System.out.println(hexString); if(hexString.length()<2){ hexString = "0"+hexString; } sb.append(hexString); return sb.toString();//MD5是32位的,返回加密后的字符串。。此算法不可逆 //MD5在线查询破解 www.cmd5.com..只是对一些常用的密码可以解密 } }catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block
//没有该算法时 抛出异常
e.printStackTrace(); } return ""; } //获取到文件的MD5 public static String getFileMd5(String sourceDir) throws Exception { // TODO Auto-generated method stub File file = new File(sourceDir); StringBuffer sb = new StringBuffer(); try { FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int len = -1; //获取到数字摘要 MessageDigest messageDigest = MessageDigest.getInstance("md5"); while((len = fis.read(buffer)) != -1){ messageDigest.update(buffer,0,len); } byte[] result = messageDigest.digest(); for(byte b:result){ int i = b&0xff; String hexString = Integer.toHexString(i); System.out.println(hexString); if(hexString.length()<2){ hexString = "0"+hexString; } sb.append(hexString); } }catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sb.toString(); }}
知识点:
GirdView + BaseAdapter+点击事件
自定义TextView