万书网 > 文学作品 > Android从入门到精通 > 第173页

第173页





break;



case  4:



       //使用Showinfo窗口初始化Intent





intent=new  Intent(MainActivity.this,  Showinfo.class);



startActivity(intent);  //打开Showinfo



break;



case  5:



intent=new  Intent(MainActivity.this,  Sysset.class);  //使用Sysset窗口初始化Intent



startActivity(intent);  //打开Sysset



break;



case  6:



       //使用Accountflag窗口初始化Intent





intent=new  Intent(MainActivity.this,  Accountflag.class);



startActivity(intent);  //打开Accountflag



break;



case  7:



finish();  //关闭当前Activity



}



}



});



}

15.9.3 定义文本及图片组件

定义一个ViewHolder类,用来定义文本组件及图片组件对象,代码如下:

class  ViewHolder  //创建ViewHolder类



{



public  TextView  title;  //创建TextView对象



public  ImageView  image;  //创建ImageView对象



}

15.9.4 定义功能图标及说明文字

定义一个Picture类,用来定义功能图标及说明文字的实体,代码如下:

class  Picture  //创建Picture类



{



private  String  title;  //定义字符串,表示图像标题



private  int  imageId;  //定义int变量,表示图像的二进制值



public  Picture()  //默认构造函数



{



super();



}



public  Picture(String  title,int  imageId)  //定义有参构造函数



{



super();



this.title=title;  //为图像标题赋值



this.imageId=imageId;  //为图像的二进制值赋值



}



public  String  getTitle()  {  //定义图像标题的可读属性



return  title;



}



public  void  setTitle(String  title)  {  //定义图像标题的可写属性



this.title=title;



}



public  int  getImageId()  {  //定义图像二进制值的可读属性



return  imageId;



}



public  void  setimageId(int  imageId)  {  //定义图像二进制值的可写属性



this.imageId=imageId;



}



}

15.9.5 设置功能图标及说明文字

定义一个pictureAdapter类,该类继承自BaseAdapter类,用来为ViewHolder类中的TextView和ImageView组件设置功能图标及说明性文字,代码如下:

class  pictureAdapter  extends  BaseAdapter  //创建基于BaseAdapter的子类



{



private  LayoutInflater  inflater;  //创建LayoutInflater对象



private  List  pictures;  //创建List泛型集合



//为类创建构造函数



public  pictureAdapter(String[]  titles,int[]  images,Context  context)  {



super();



pictures=new  ArrayList();  //初始化泛型集合对象



inflater=LayoutInflater.from(context);  //初始化LayoutInflater对象



for(int  i=0;i


{



Picture  picture=new  Picture(titles[i],  images[i]);  //使用标题和图像生成Picture对象



pictures.add(picture);  //将Picture对象添加到泛型集合中



}



}



@Override



public  int  getCount()  {  //获取泛型集合的长度



//TODO  Auto-generated  method  stub



if  (null  !=  pictures)  {  //如果泛型集合不为空



return  pictures.size();  //返回泛型长度



}



else  {



return  0;  //返回0



}



}



@Override



public  Object  getItem(int  arg0)  {



//TODO  Auto-generated  method  stub



return  pictures.get(arg0);  //获取泛型集合指定索引处的项



}



@Override



public  long  getItemId(int  arg0)  {



//TODO  Auto-generated  method  stub



return  arg0;  //返回泛型集合的索引



}



@Override



public  View  getView(int  arg0,  View  arg1,  ViewGroup  arg2)  {



//TODO  Auto-generated  method  stub



ViewHolder  viewHolder;  //创建ViewHolder对象



if(arg1==null)  //判断图像标识是否为空



{



arg1=inflater.inflate(R.layout.gvitem,  null);  //设置图像标识



viewHolder=new  ViewHolder();  //初始化ViewHolder对象



viewHolder.title=(TextView)  arg1.findViewById(R.id.ItemTitle);  //设置图像标题



viewHolder.image=(ImageView)  arg1.findViewById(R.id.ItemImage);  //设置图像的二进制值



arg1.setTag(viewHolder);  //设置提示