手机安全卫士学习(2)

今天是安全卫士学习第二天,主要涉及以下内容:

1 安全卫士主页面的布局

  其中涉及gridview的使用,包括布局文件的引用,以及自定义控件textview

<?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:gravity="center"
        android:background="#8866ff00"
        android:id="@+id/tv_function_list"
        android:layout_width="fill_parent"
        android:layout_height="55dip"
        android:text="功能列表"
        android:textSize="22sp"
        android:textColor="#000000"/>
    <com.djf.mobilesafty.ui.FocusdTextView
        android:layout_marginTop="10dip"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:textSize="22sp"
        android:text="距离季前赛还有11天,欲知详细信息,请登录查看详细信息,新赛季更精彩,值得期待··"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <GridView
        android:verticalSpacing="30dip"
        android:layout_marginTop="60dip"
        android:numColumns="3"
        android:id="@+id/gv_item_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

package com.djf.mobilesafty;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeActivity extends Activity {
 private GridView gv_item_home;
 private Myadapter adapter;
 private String[] names  = {
   "手机防盗","通讯卫士","软件管理",
   "进程管理","流量统计","手机杀毒",
   "缓存清理","高级工具","设置中心"};
 private static int[] ids = {
  R.drawable.safe,R.drawable.callmsgsafe,R.drawable.app,
  R.drawable.taskmanager,R.drawable.netmanager,R.drawable.trojan,
  R.drawable.sysoptimize,R.drawable.atools,R.drawable.settings
  
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_home);
  gv_item_home = (GridView) findViewById(R.id.gv_item_home);
  adapter = new Myadapter();
  gv_item_home.setAdapter(adapter);
  gv_item_home.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    // TODO Auto-generated method stub
    switch (position) {
    case 8:
     Intent intent = new Intent(HomeActivity.this,SetActivity.class);
     
     startActivity(intent);
     break;

    default:
     break;
    }
   }
  });
 }
 public class  Myadapter extends BaseAdapter{

  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return names.length;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   View view = View.inflate(HomeActivity.this, R.layout.list_item_home, null);
   TextView  tv_item = (TextView) view.findViewById(R.id.tv_item);
   ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
   tv_item.setText(names[position]);
   iv_item.setImageResource(ids[position]);
   return view;
  }
  @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return null;
  }

  @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return 0;
  }

  
 }

}

2 设置中心的布局,

其中涉及自定义组件,其中组件中涉及两个textview,一个checkbox,一个view,以及对自定义组件的动作处理,信息的改变

package com.djf.mobilesafty;

import com.djf.mobilesafty.ui.SetItemView;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class SetActivity extends Activity {
 private SetItemView setItemView;
 private SharedPreferences sp;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_set);
  sp = getSharedPreferences("config", MODE_PRIVATE);
  
  setItemView = (SetItemView) findViewById(R.id.siv_update);
  boolean update = sp.getBoolean("update", false);
  if (update) {
   //自动升级已经开启
   setItemView.SetChecked(true);
   setItemView.SetDesc("开启升级");
  }
  else {
   //自动升级已经关闭
   setItemView.SetChecked(false);
   setItemView.SetDesc("关闭升级");
  }
  setItemView.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Editor editor = sp.edit();
    // 组件已经被选中
    if (setItemView.isChecked()) {
     // 设置组件处于非选中状态,关闭升级
     setItemView.SetChecked(false);
     setItemView.SetDesc("关闭升级");
     editor.putBoolean("update", false);
    } else // 组件没有被选中
    {
     setItemView.SetChecked(true);
     setItemView.SetDesc("开启升级");
     editor.putBoolean("update", true);
    }
    editor.commit();
   }
  });
 }
}

自定义组件布局

<?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:layout_width="fill_parent"
        android:layout_height="55dip"
        android:background="#8866ff00"
        android:gravity="center"
        android:text="设置中心"
        android:textColor="#000000"
        android:textSize="22sp" />

    <com.djf.mobilesafty.ui.SetItemView
        android:id="@+id/siv_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </com.djf.mobilesafty.ui.SetItemView>

</LinearLayout>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。