activity_home.xml home_list_item.xml style.xml color.xml HomeActivity.java 知識點: GirdView + BaseAdapter+點擊事件 自定義TextView ...
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