android常用控件(三)- ProgressBar、ListView

来源:互联网 发布:linux 安全设置 编辑:程序博客网 时间:2024/06/01 08:11

Android常用控件(三)- ProgressBar、ListView 
一、ProgressBar(进度条)的使用 
示例1:一个应用程序有2个ProgressBar,让进度条显示进度。 
开发步骤: 
1、新建一个android项目 
2、在main.xml布局文件中先添加1个ProgressBar控件(firstProgressBar),设置这个进度条是以水平方式展示的,然后再设置这个控件为不显示(这里暂时不显示,我们在后面的程序中写代码将这个控件设置为显示) 
3、在main.xml布局文件中再添加1个ProgressBar控件(secondProgressBar),设置该标签的显示样式为默认的(是一个转动的圆圈),然后再同样设置这个控件暂时为不显示 
4、在main.xml布局文件中再添加一个Button控件 
5、在Activity中编写代码,先获得这2个ProgressBar和Button对象 
6、然后编写一个监听器,设置进度条的进度,当每点击一次Button,进度则增加10 
7、将监听器绑定到Button对象上 
下图为main.xml布局文件的片段: 

 

Activity的代码在这里就没有贴出来了,项目源码已经上传,有需要的可以下载。 
最后项目实现的效果为: 

 

二、ListView的使用 
ListView的使用相对于之前使用到的一些控件而言要复杂一点,在这里呢,同样是通过一个示例来讲解,但是在这过程中,可能有些理解没有很准确或者注释的不是准确的地方,到时候有什么问题还请大家多多指点指点哦~ 
(如果各位E文比较好的可以去官网查看更详细的说明) 
示例2:一个应用程序有一个ListView,显示三行信息。 
最后项目实现的效果为: 

 

开发步骤: 
1、新建一个Android应用程序 
2、在布局文件中再添加一个LinearLayout(应用程序新建的时候默认的布局文件里就已经有了一个LinearLayout,现在再添加一个),设置这个LinearLayout的一些属性。 
3、在LinearLayout中添加一个ListView,并设置一些属性。 

 

4、新建一个布局文件,这个布局文件呢,是用来布局和显示ListView里面的内容的(我是这样理解的,不知道大家能不能理解哈)。先在这个布局文件中添加一个LinearLayout,然后在LinearLayout中添加两个TextView,表示显示两列数据(如果需要显示多列数据的话当然就是添加多个TextView了)。 

Xml代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:paddingTop="1dip"  
  7.     android:paddingBottom="1dip"  
  8.     android:paddingLeft="10dip"  
  9.     android:paddingRight="10dip"  
  10. >  
  11.     <TextView  
  12.         android:id="@+id/user_name"  
  13.         android:layout_width="180dip"  
  14.         android:layout_height="30dip"  
  15.         android:textSize="10pt"  
  16.         android:singleLine="true"  
  17.     />  
  18.     <TextView  
  19.         android:id="@+id/user_ip"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:textSize="10pt"  
  23.         android:gravity="right"  
  24.     />  
  25. </LinearLayout>  


5、编写Activity 
a)首先要注意的是,这个Activity他继承的不是Activity,而是ListActivity。 
b)创建一个ArrayList,里面存放的是HashMap,而HashMap的键值对都是String类型。 
c)分别往3个HashMap中存储值 
d)将3个HashMap添加到ArrayList中 
e)创建适配器 
f)绑定到适配器 
g)编写行的点击事件 
Java代码 
  1. package android.listview;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5.   
  6. import android.app.ListActivity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.ListView;  
  10. import android.widget.SimpleAdapter;  
  11.   
  12. //注意:这里继承的是ListActivity,而不是Activity  
  13. public class ListViewTest extends ListActivity {  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.   
  20.         // 创建一个ArrayList,ArrayList里面存放的是HashMap,而HashMap的键值对都是String类型  
  21.         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();  
  22.         HashMap<String, String> map1 = new HashMap<String, String>();  
  23.         HashMap<String, String> map2 = new HashMap<String, String>();  
  24.         HashMap<String, String> map3 = new HashMap<String, String>();  
  25.         // 分别往3个HashMap中存储值  
  26.         map1.put("user_name""zhangsan");  
  27.         map1.put("user_ip""192.168.0.1");  
  28.         map2.put("user_name""lisi");  
  29.         map2.put("user_ip""192.168.0.2");  
  30.         map3.put("user_name""wangwu");  
  31.         map3.put("user_ip""192.168.0.3");  
  32.         // 将3个HashMap添加到ArrayList中  
  33.         list.add(map1);  
  34.         list.add(map2);  
  35.         list.add(map3);  
  36.         //创建适配器  
  37.         //第一个参数Content:上下文  
  38.         //第二个参数List<? extends Map<String, ?>>:ArrayList对象,ArrayList里面存放的是HashMap,而HashMap的键值对都是String类型  
  39.         //第三个参数int resource:内容显示的布局文件  
  40.         //第四个参数String[] from:被添加到ArrayList中的HashMap中key的名称,要显示的列  
  41.         //第五个参数int[] to:内容显示的布局文件中,显示内容的控件id  
  42.         SimpleAdapter listAdapter = new SimpleAdapter(this, list,  
  43.                 R.layout.user, new String[] { "user_name""user_ip" },  
  44.                 new int[] { R.id.user_name, R.id.user_ip });  
  45.         //绑定到适配器。  
  46.         setListAdapter(listAdapter);  
  47.     }  
  48.   
  49.     /** 
  50.      * 列表当中一行的点击事件 
  51.      * ListView:ListView对象本身 
  52.      * View:被选中的那一行的View对象 
  53.      * position:被选中的那一行的位置 
  54.      * id:被选中的那一行的id 
  55.      */  
  56.     @Override  
  57.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  58.         // TODO Auto-generated method stub  
  59.         super.onListItemClick(l, v, position, id);  
  60.         //打印出被选中的那一行的位置和id,计数都是从0开始  
  61.         System.out.println("-------------"+position);  
  62.         System.out.println("-------------"+id);  
  63.     }  
  64. }  

from:http://byandby.javaeye.com/blog/831539

原创粉丝点击