控件之 Dialog ListView GridView

Dialog 对话框,它运行起来的效果是什么样呢?如下图
1.png
2011-12-20 18:45 上传
下载附件 (28.95 KB)

这种是最常用的对话框
2.png
2011-12-20 18:46 上传
下载附件 (36.5 KB)

当点击了上图的确定后,会弹此对话框,这种对话框属于自定义布局类型
3.png
2011-12-20 18:46 上传
下载附件 (26.3 KB)

当执行一些比较费时的操作时,用这种对话框是个不错的选择
4.png
2011-12-20 18:47 上传
下载附件 (14.64 KB)

当我们需要用户进行选择操作,又不能使用下来列表时,可以使用这种自定义布局的对话框
接下来我们就一起来看看这些通过代码是如何实现的?
  1. package TSD.Jason.Example;

  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.ProgressDialog;
  5. import android.app.AlertDialog.Builder;
  6. import android.app.Dialog;
  7. import android.content.DialogInterface;
  8. import android.os.Bundle;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.widget.Button;

  12. /**
  13. * 对话框
  14. * @author Administrator
  15. * 常用方法:
  16. *     setTitle():给对话框设置title.
  17.     setIcon():给对话框设置图标。
  18.     setMessage():设置对话框的提示信息
  19.     setItems():设置对话框要显示的一个list,一般用于要显示几个命令时
  20.     setSingleChoiceItems():设置对话框显示一个单选的List
  21.     setMultiChoiceItems():用来设置对话框显示一系列的复选框。
  22.     setPositiveButton():给对话框添加”Yes”按钮。
  23.     setNegativeButton():给对话框添加”No”按钮。
  24. *
  25. */
  26. publicclass DialogActivity extends Activity {
  27.     Button btn1;
  28.     ProgressDialog p;
  29.     @Override
  30.     protectedvoid onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.diaolg);
  33.         btn1 = (Button)findViewById(R.id.btnDialog);
  34.         btn1.setOnClickListener(new Button.OnClickListener() {
  35.             @Override
  36.             publicvoid onClick(View v) {
  37.                 Builder dialog = new AlertDialog.Builder(DialogActivity.this);
  38.                 dialog.setTitle("登录提示");
  39.                 dialog.setIcon(android.R.drawable.ic_dialog_info);
  40.                 dialog.setMessage("是否登录?");
  41.                 dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  42.                     @Override
  43.                     publicvoid onClick(DialogInterface dialog, int which) {
  44.                         ShowLoginDialog();
  45.                     }
  46.                 });
  47.                 dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  48.                     @Override
  49.                     publicvoid onClick(DialogInterface dialog, int which) {
  50.                         DialogActivity.this.finish();
  51.                     }
  52.                 });
  53.                 dialog.show();
  54.             }
  55.         });
  56.     }
  57. }
复制代码
以上代码是我们第一张对话框的效果实现代码,大家发现是不是挺简单,当我们单击确定按钮后,将调用一个叫做ShowLoginDialog的方法。
这个方法马上会贴出来,在这里我还是要强调下,大家在写代码的时候一定要有一个良好的编程思想,将功能拆分,降低代码的耦合度,一个方法只做一件事情,不要一股脑的将代码写到一个方法里。希望大家记得。
  1. privatevoid ShowLoginDialog()
  2.     {
  3.         Builder builder =new AlertDialog.Builder(DialogActivity.this);
  4.         builder.setTitle("用户登录");
  5.         LayoutInflater factory = LayoutInflater.from(DialogActivity.this);
  6.         View dialogView  = factory.inflate(R.layout.dialogview, null);
  7.         builder.setView(dialogView);
  8.         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  9.             @Override
  10.             publicvoid onClick(DialogInterface dialog, int which) {
  11.                 DialogWait();
  12.             }
  13.         });
  14.         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  15.             @Override
  16.             publicvoid onClick(DialogInterface dialog, int which) {
  17.                 DialogActivity.this.finish();
  18.             }
  19.         });
  20.         builder.show();
  21.     }
复制代码
上边被标注的代码是为了让Dialog中的内容部分显示我们自定义的布局文件,通过builder对象的setView方法就可以将R.layout.dialogview这个布局文件绑定到对话框中。
布局文件代码如下
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView 
  8.     android:layout_width="fill_parent"
  9.     android:layout_height="wrap_content"
  10.     android:text="账号"
  11.     />
  12. <EditText
  13.     android:id="@+id/txtUserName"
  14.     android:layout_width="fill_parent"
  15.     android:layout_height="wrap_content"
  16.     android:hint="请输入账号"
  17. />
  18. <TextView 
  19.     android:layout_width="fill_parent"
  20.     android:layout_height="wrap_content"
  21.     android:text="密码"
  22.     />
  23. <EditText
  24.     android:id="@+id/txtUserPwd"
  25.     android:layout_width="fill_parent"
  26.     android:layout_height="wrap_content"
  27.     android:password="true"
  28.     android:hint="请输入密码"
  29. />
  30. </LinearLayout>
复制代码
第三个等待效果的对话框是如何实现的呢?上边我们调用了一个方法叫做 DialogWait

  1. privatevoid DialogWait()
  2.     {
  3.         p = ProgressDialog.show(DialogActivity.this, "正在登录", "请稍后...", true);
  4.         new Thread(){
  5.             publicvoid run(){
  6.                 try {
  7.                     Thread.sleep(2000);
  8.                 } catch (InterruptedException e) {
  9.                     // TODO Auto-generated catch block
  10.                     e.printStackTrace();
  11.                 }
  12.                 finally{
  13.                     p.dismiss();//登录结束后,取消对话框
  14.                 }
  15.             }
  16.         }.start();
  17.        
  18.     }
复制代码
大家注意,此时的类就不在是AlertDialog,而是ProgressDialog。
上边代码我们为了测试看效果,所以开启了一个线程,并挂起2秒,这在以后项目中是不需要的,如果大家看不太懂,那么这里先跳过。
接下来我们来看看如何在对话框中嵌套一个ListView。
首先,需要一个布局文件,布局文件里只创建一个ListView,如下代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent" android:scrollbars="vertical">
  5.       <ListView
  6.             android:id="@+id/listCity"
  7.             android:layout_width="fill_parent"
  8.             android:layout_height="fill_parent"
  9.             android:scrollbars="vertical"/>
  10. </LinearLayout>
复制代码
Java代码如下
  1. privatevoid ShowLoginDialog()
  2.     {
  3.         Builder builder =new AlertDialog.Builder(Tab1Activity.this);
  4.         builder.setTitle("选择城市");
  5.         LayoutInflater factory = LayoutInflater.from(Tab1Activity.this);
  6.         View dialogView  = factory.inflate(R.layout.dialogcity, null);
  7.         listCity =(ListView)dialogView.findViewById(R.id.listCity);
  8.         GetCity();
  9.         builder.setView(dialogView);
  10.         builder.show();
  11.     }

  12. privatevoid GetCity()
  13.     {
  14.         System.out.println("asd");
  15.         ArrayList<HashMap<String, String>> listData =new ArrayList<HashMap<String,String>>();
  16.         HashMap<String, String> hmItem =new HashMap<String, String>();
  17.         hmItem.put("city", "北京");
  18.         listData.add(hmItem);
  19.         hmItem =new HashMap<String, String>();
  20.         hmItem.put("city", "上海");
  21.         listData.add(hmItem);
  22.         hmItem =new HashMap<String, String>();
  23.         hmItem.put("city", "深圳");
  24.         listData.add(hmItem);
  25.         hmItem =new HashMap<String, String>();
  26.         hmItem.put("city", "天津");
  27.         listData.add(hmItem);
  28.         hmItem =new HashMap<String, String>();
  29.         hmItem.put("city", "南京");
  30.         listData.add(hmItem);
  31.         hmItem =new HashMap<String, String>();
  32.         hmItem.put("city", "武汉");
  33.         listData.add(hmItem);
  34.         hmItem =new HashMap<String, String>();
  35.         hmItem.put("city", "江苏");
  36.         listData.add(hmItem);
  37.         hmItem =new HashMap<String, String>();
  38.         hmItem.put("city", "宁波");
  39.         listData.add(hmItem);
  40.         SimpleAdapter sim =new SimpleAdapter(this, listData, android.R.layout.simple_list_item_1, new String[]{"city"}, newint[]{android.R.id.text1});
  41.         listCity.setAdapter(sim);
  42.     }
复制代码
直接调用ShowLoginDialog方法即可。注意标注的代码,需要先获取ListView。这里已经用到了ListView,如果不太懂下边就将ListView,大家注意看。
ListView
上边已经展示过它运行的效果了,这里就不展示运行效果了。
那么要使用ListView需要哪些步骤呢?举一个例子,可能不太恰当
冰箱里没有鸡蛋了,我们从家里提了一个篮子去超市买鸡蛋。就是这样的一个过程。我们来分解下这个步骤
冰箱 == 展示数据 == ListView
超市里的鸡蛋 == 数据 == ArrayList<E> 泛型集合
篮子 == 适配器 == SimpleAdapter
我们应该将 鸡蛋(ArrayList<E>) 装到 篮子里(SimpleAdapter) 然后提回家 放到 冰箱里( ListView)
分解完步骤后,那么我们看看如何用代码实现这个过程。
ListView userList; //声明一个ListView对象(冰箱)
userList = (ListView)findViewById(R.id.listUserInfo); //获取布局文件中的ListView控件赋值给ListView对象
ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String,String>>(); //数据源 (超市装鸡蛋的盒子)
HashMap<String, String> hmItem = new HashMap<String, String>(); //需要一个HashMap键值对 (每一个鸡蛋)
  hmItem.put("userName", "张三");
  hmItem.put("userPhone", "1234567890");
  listData.add(hmItem); //将鸡蛋装到数据源中
String[] s = new String[2]; //列 和键值对中的键 一一对应 每个键值对应该是一样的列数
  s[0] = "userName";
  s[1] = "userPhone";
  int[] i = new int[2]; //用什么控件来装载上边String集合中的列 和上边的String数组也是一一对应的
  i[0] = android.R.id.text1;
  i[1] = android.R.id.text2;
  SimpleAdapter sim = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_1, s, i); //这就是我们的篮子
  userList.setAdapter(sim); //将篮子中的鸡蛋装到冰箱中 :)
完整的代码如下
  1. package TSD.Jason.Example;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;

  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.ListView;
  10. import android.widget.SimpleAdapter;

  11. /**
  12. * ListView基本使用方法
  13. *
  14. * 使用ListView的基本步骤
  15. * 1.准备ListView要显示的数据 ;
  16. * ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String,String>>();
  17. *
  18. * 2.使用 一维或多维 动态数组 保存数据;
  19. * HashMap<String, String> hmItem = new HashMap<String, String>();
  20.         hmItem.put("userName", "张三");
  21.         hmItem.put("userPhone", "1234567890");
  22.        
  23. * 3.构建适配器 , 简单地来说, 适配器就是 Item数组 , 动态数组 有多少元素就生成多少个Item;
  24. * SimpleAdapter simpleAdapter;
  25. * 数据绑定的类
  26. * 参数解释
  27. *
  28. * 第一个context,很明显大家根据英文可以知道是上下文的意思,它官方的意思是:SimpleAdapter所要运行关联到的视图,这个是什么呢?就是你这个SimpleAdapter所在的Activity(一般而言),所以这个参数一般是this

  29. 第二个是一个泛型只要是一个List就行,这一般会想到是ArrayList,而他内部存储的则是Map或者继承自Map的对象,比如HashMap,这些语法都是Java的基本语法,不再详述了!这里呢是作为数据源,而且每一个ArraList中的一行就代表着呈现出来的一行,Map的键就是这一行的列名,值也是有列名的。

  30. 第三个资源文件,就是说要加载这个两列所需要的视图资源文件,你可以左边一个TextView右边一个TextView,目的在于呈现左右两列的值!

  31. 第四个参数是一个数组,主要是将Map对象中的名称映射到列名,一一对应

  32. 第五个是将第四个参数的值一一对象的显示(一一对应)在接下来的int形的id数组中,这个id数组就是LayOut的xml文件中命名id形成的唯一的int型标识符

  33. *  context   关联SimpleAdapter运行着的视图的上下文。
  34.     data        一个Map的列表。在列表中的每个条目对应列表中的一行,应该包含所有在from中指定的条目
  35.     resource              一个定义列表项目的视图布局的资源唯一标识。布局文件将至少应包含哪些在to中定义了的名称。
  36.     from       一个将被添加到Map上关联每一个项目的列名称的列表
  37.     to    应该在参数from显示列的视图。这些应该全是TextView。在列表中最初的N视图是从参数from中最初的N列获取的值。
  38. *
  39. * 4.把 适配器 添加到ListView,并显示出来。
  40. * @author Administrator
  41. *
  42. */
  43. publicclass ListViewActivity extends Activity {
  44.    
  45.     ListView userList;
  46.     @Override
  47.     protectedvoid onCreate(Bundle savedInstanceState) {
  48.         super.onCreate(savedInstanceState);
  49.         setContentView(R.layout.listview);
  50.         userList = (ListView)findViewById(R.id.listUserInfo);

  51.         ArrayList<HashMap<String, String>> listData =new ArrayList<HashMap<String,String>>();

  52.         HashMap<String, String> hmItem =new HashMap<String, String>();
  53.         hmItem.put("userName", "张三");
  54.         hmItem.put("userPhone", "1234567890");
  55.         listData.add(hmItem);
  56.        
  57.        
  58.         hmItem =new HashMap<String, String>();
  59.         hmItem.put("userName", "李四");
  60.         hmItem.put("userPhone", "981234502");
  61.         listData.add(hmItem);
  62.        
  63.        
  64.        
  65.        
  66.         hmItem =new HashMap<String, String>();
  67.         hmItem.put("userName", "王五");
  68.         hmItem.put("userPhone", "5622435566221");
  69.         listData.add(hmItem);
  70.        
  71.        
  72.        
  73.        
  74.        
  75.         //SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, R.layout.textviewitem, new String[]{"userName","userPhone"}, new int[]{R.id.txtUserName,R.id.txtUserPhone});
  76.         //SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_1, new String[]{"userName","userPhone"}, new int[]{android.R.id.text1,android.R.id.text2});
  77.         //SimpleAdapter simpleAdapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_2, new String[]{"userName","userPhone"}, new int[]{android.R.id.text1,android.R.id.text2});
  78.        
  79.         String[] s =new String[2];
  80.         s[0] ="userName";
  81.         s[1] ="userPhone";
  82.         int[] i =newint[2];
  83.         i[0] = android.R.id.text1;
  84.         i[1] = android.R.id.text2;
  85.         SimpleAdapter sim =new SimpleAdapter(this, listData, android.R.layout.simple_list_item_1, s, i);
  86.        
  87.         userList.setAdapter(sim);
  88.        
  89.         //列表项单击事件
  90.         userList.setOnItemClickListener(new ListView.OnItemClickListener() {
  91.             @Override
  92.             publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  93.                     long arg3) {
  94.                 System.out.println(arg2);
  95.                 System.out.println(arg3);
  96.             }
  97.         });
  98.        
  99.         //列表项选中事件
  100.         userList.setOnItemSelectedListener(new ListView.OnItemSelectedListener() {

  101.             @Override
  102.             publicvoid onItemSelected(AdapterView<?> arg0, View arg1,
  103.                     int arg2, long arg3) {
  104.                 // TODO Auto-generated method stub
  105.                 System.out.println("selected----------"+arg2);
  106.                 System.out.println("selected----------"+arg3);
  107.             }

  108.             @Override
  109.             publicvoid onNothingSelected(AdapterView<?> arg0) {
  110.                 // TODO Auto-generated method stub
  111.                
  112.             }
  113.         });
  114.        
  115.         //列表项长按事件
  116.         userList.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {

  117.             @Override
  118.             publicboolean onItemLongClick(AdapterView<?> arg0, View arg1,
  119.                     int arg2, long arg3) {
  120.                 System.out.println("long---------"+ arg2);
  121.                 System.out.println("long---------"+ arg3);
  122.                 returntrue;
  123.             }
  124.         });
  125.     }
  126. }
复制代码
上边注释的三句话
第一句 是我们可以自定义布局文件展示数据
第二句 我们可以用内置的布局文件来展示
第三句 和第二句一样,但是效果不一样,大家运行看看就明白了
GridView
类似与手机主菜单中展示的效果,如图
5.png
2011-12-20 18:49 上传
下载附件 (75.72 KB)
网格视图控件和我们的ListView 操作很像,上边已经解释过了,这里直接贴代码了
  1. package TSD.Jason.Example;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;

  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.widget.GridView;
  7. import android.widget.SimpleAdapter;

  8. publicclass GridViewActivity extends Activity {

  9.     // 定义整型数组 即图片源
  10. private Integer[]    mImageIds    =
  11.     {
  12.             R.drawable.img1,
  13.             R.drawable.img2,
  14.             R.drawable.img3,
  15.             R.drawable.img4,
  16.             R.drawable.img5,
  17.             R.drawable.img6,
  18.             R.drawable.img7,
  19.             R.drawable.img8,
  20.             R.drawable.img1,
  21.     };
  22.     @Override
  23.     protectedvoid onCreate(Bundle savedInstanceState) {
  24.         // TODO Auto-generated method stub
  25. super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.gridview);
  27.         GridView gridview = (GridView) findViewById(R.id.gridview);
  28.         // 生成动态数组,并且转入数据
  29.         ArrayList<HashMap<String, Object>> lstImageItem =new ArrayList<HashMap<String, Object>>();
  30.        
  31.         for (int i =0; i <9; i++) {
  32.             HashMap<String, Object> map =new HashMap<String, Object>();
  33.             map.put("ItemImage", mImageIds[i]);// 添加图像资源的ID
  34.             map.put("ItemText", "NO."+ String.valueOf(i));// 按序号做ItemText
  35.             lstImageItem.add(map);
  36.         }
  37.         SimpleAdapter simple =new SimpleAdapter(this, lstImageItem,
  38.                 R.layout.gridviewitem,
  39.                 new String[] { "ItemImage", "ItemText" }, newint[] {
  40.                         R.id.ItemImage, R.id.ItemText });
  41.         gridview.setAdapter(simple);
  42.     }
  43. }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值