android--UI组件总结

android的组件基本都按如下方法生成:

1、生成一个组件对象:通过xml文件或在代码中生成

2、对组件进行设置

3、添加事件监听器

View:

View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。

可以用在多个Button添加一个监听器,每个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。

 

  1. package fy.test;     
  2.     
  3. import android.app.Activity;     
  4. import android.os.Bundle;     
  5. import android.view.View;     
  6. import android.widget.Button;     
  7.     
  8. public class Main extends Activity {     
  9.     /** Called when the activity is first created. */    
  10.     @Override    
  11.     public void onCreate(Bundle savedInstanceState) {     
  12.         super.onCreate(savedInstanceState);     
  13.         setContentView(R.layout.main);     
  14.         Button button1 = (Button) findViewById(R.id.Button01);     
  15.         Button button2 = (Button) findViewById(R.id.Button02);     
  16.         Button button3 = (Button) findViewById(R.id.Button03);     
  17.         Button button4 = (Button) findViewById(R.id.Button04);     
  18.         MyListener listener = new MyListener();     
  19.         button1.setTag(1);     
  20.         button1.setOnClickListener(listener);     
  21.         button2.setTag(2);     
  22.         button2.setOnClickListener(listener);     
  23.         button3.setTag(3);     
  24.         button3.setOnClickListener(listener);     
  25.         button4.setTag(4);     
  26.         button4.setOnClickListener(listener);     
  27.     }     
  28.     
  29.     public class MyListener implements View.OnClickListener {     
  30.     
  31.         @Override    
  32.         public void onClick(View v) {     
  33.             int tag = (Integer) v.getTag();     
  34.             switch (tag) {     
  35.             case 1:     
  36.                 System.out.println("button1 click");     
  37.                 break;     
  38.             case 2:     
  39.                 System.out.println("button2 click");     
  40.                 break;     
  41.             case 3:     
  42.                 System.out.println("button3 click");     
  43.                 break;     
  44.             case 4:     
  45.                 System.out.println("button4 click");     
  46.                 break;     
  47.             }     
  48.         }     
  49.     
  50.     }     
  51.     
  52. }    

 

main.xml

 

  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="@string/hello"    
  11.     />    
  12.     <Button      
  13.     android:text="Button01"      
  14.     android:id="@+id/Button01"      
  15.     android:layout_width="wrap_content"      
  16.     android:layout_height="wrap_content">    
  17.     </Button>    
  18.     <Button      
  19.         android:text="Button02"      
  20.         android:id="@+id/Button02"      
  21.         android:layout_width="wrap_content"      
  22.         android:layout_height="wrap_content">    
  23.     </Button>    
  24.     <Button      
  25.         android:text="Button03"      
  26.         android:id="@+id/Button03"      
  27.         android:layout_width="wrap_content"      
  28.         android:layout_height="wrap_content">    
  29.     </Button>    
  30.     <Button      
  31.         android:text="Button04"      
  32.         android:id="@+id/Button04"      
  33.         android:layout_width="wrap_content"      
  34.         android:layout_height="wrap_content">    
  35.     </Button>    
  36. </LinearLayout>  
  37.   
  38. 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenghome/archive/2010/06/20/5681338.aspx  

 

ListView:

1、生成ListView对象:可以在Layout中声明,也可以在代码中声明

2、设置ListView对象的各个属性。主要是Adatper

3、设置监听器

Adapter:

Adapter,可以理解为一种pb中的结果集(数据源+界面):


ArrayAdapter数据源:一维数组,界面:系统提供或自定义 

数据源:数组 :String[] names = {"张三","李四"};

界面:

      系统提供多种:

            android.R.Layout.simple_list_item_1

            android.R.Layout.simple_list_item_2

            android.R.Layout.simple_list_item_checked

            android.R.Layout.simple_list_item_multiple_choice

            android.R.Layout.simple_list_item_single_choice

           

     自定义:  是一个layout

 绑定:ArrayAdapther   adapter = new ArrayAdapter<String>(this,界面布局,数据源);

SimpleAdapter 数据源:多维数据 界面:系统提供多种或自定义

     数据源:Map负责一行的每列,ArrayList负责所有行

     界面:一个Layout或程序指定。

SimpleCursorAdapter 数据源:Cursor 界面:系统提供多种或自定义

BaseAdapter:自定义数据源与界面的关联方式,自定义行界面各组件的时间响应。

     框架流程:

     1、Activity显示主布局,发现包含ListView就绘制ListView

     2、ListView绘制时会查找自己身上的Adapter,调用Adapter的getSize()取得行号,并逐行调用getView()取得“行view”并将其画出来。

     3、Adapter负责将数据源与行界面相关联。

数据源:自定义

界面:自定义

       先生成行界面

       生成需要显示的数据

       生成一个BaseAdapter的子类,

                  实现getCount() 方法:返回数据源的行数

                  实现getView()方法:设置数据与行View关联,设置组件事件响应。

 

  1. package fy.test;     
  2.     
  3. import java.util.ArrayList;     
  4. import java.util.HashMap;     
  5. import java.util.Map;     
  6.     
  7. import android.app.Activity;     
  8. import android.content.Context;     
  9. import android.os.Bundle;     
  10. import android.view.LayoutInflater;     
  11. import android.view.View;     
  12. import android.view.ViewGroup;     
  13. import android.view.View.OnClickListener;     
  14. import android.widget.BaseAdapter;     
  15. import android.widget.Button;     
  16. import android.widget.ListView;     
  17. import android.widget.TextView;     
  18.     
  19. public class HellowListView extends Activity {     
  20.     private ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();     
  21.     private Map<String, Object> map;     
  22.     
  23.     /** Called when the activity is first created. */    
  24.     @Override    
  25.     public void onCreate(Bundle savedInstanceState) {     
  26.         super.onCreate(savedInstanceState);     
  27.         setContentView(R.layout.main);     
  28.         initDate();     
  29.         MyAdapter adapter = new MyAdapter(this);     
  30.         ListView list = (ListView) findViewById(R.id.ListView01);     
  31.         list.setAdapter(adapter);     
  32.     }     
  33.     
  34.     private void initDate() {     
  35.         map = new HashMap<String, Object>();     
  36.         map.put("姓名""张三");     
  37.         map.put("电话""1132434343333");     
  38.         arrayList.add(map);     
  39.         map.put("姓名""张三");     
  40.         map.put("电话""1132434343333");     
  41.         arrayList.add(map);     
  42.         map.put("姓名""张三");     
  43.         map.put("电话""1132434343333");     
  44.         arrayList.add(map);     
  45.     }     
  46.     
  47.     public class MyAdapter extends BaseAdapter {     
  48.         private LayoutInflater inflater;     
  49.     
  50.         public MyAdapter(Context c) {     
  51.             this.inflater = LayoutInflater.from(c);     
  52.         }     
  53.     
  54.         @Override    
  55.         public int getCount() {     
  56.             return arrayList.size();     
  57.         }     
  58.     
  59.         @Override    
  60.         public Object getItem(int arg0) {     
  61.             // TODO Auto-generated method stub     
  62.             return null;     
  63.         }     
  64.     
  65.         @Override    
  66.         public long getItemId(int arg0) {     
  67.             // TODO Auto-generated method stub     
  68.             return 0;     
  69.         }     
  70.     
  71.         /**   
  72.          * 设置数据源与行View关联   
  73.          * 设置行中个组件的事件响应   
  74.          * 返回设置好的View   
  75.          */    
  76.         @Override    
  77.         public View getView(int arg0, View arg1, ViewGroup arg2) {     
  78.             //取得要显示的行View     
  79.             View myView = inflater.inflate(R.layout.list_item, null);     
  80.             TextView name = (TextView) myView.findViewById(R.id.TextView01);     
  81.             TextView phoneNum = (TextView) myView.findViewById(R.id.TextView02);     
  82.             Button button = (Button) myView.findViewById(R.id.Button01);     
  83.             //让行View的每个组件与数据源相关联     
  84.             name.setText((String) arrayList.get(arg0).get("姓名"));     
  85.             phoneNum.setText((String) arrayList.get(arg0).get("电话"));     
  86.             final String note = "按下的是" + arg0;     
  87.             button.setFocusable(false);     
  88.             //添加事件响应     
  89.             button.setOnClickListener(new OnClickListener() {     
  90.                 @Override    
  91.                 public void onClick(View v) {     
  92.                     // TODO Auto-generated method stub     
  93.                     System.out.println(note);     
  94.                 }     
  95.             });     
  96.             return myView;     
  97.         }     
  98.     }     
  99.     
  100. }    

 

main.xml

 

  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="@string/hello"    
  11.     />    
  12.     <EditText      
  13.         android:text="aaaaaaaa"      
  14.         android:id="@+id/EditText01"      
  15.         android:layout_width="wrap_content"      
  16.         android:layout_height="wrap_content">    
  17.     </EditText>    
  18.     <ListView      
  19.         android:id="@+id/ListView01"      
  20.         android:layout_height="fill_parent"      
  21.         android:layout_width="fill_parent">    
  22.     </ListView>    
  23. </LinearLayout>    

 

sime_list.x ml

 

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout      
  3.     android:id="@+id/LinearLayout01"    
  4.     android:layout_width="fill_parent"      
  5.     android:layout_height="fill_parent"    
  6.     xmlns:android="http://schemas.android.com/apk/res/android"    
  7.     android:orientation="horizontal">    
  8.     <TextView      
  9.         android:id="@+id/TextView01"      
  10.         android:layout_width="wrap_content"    
  11.         android:layout_height="wrap_content">    
  12.         </TextView>    
  13.     <TextView      
  14.         android:id="@+id/TextView02"    
  15.         android:layout_width="wrap_content"      
  16.         android:layout_height="wrap_content">    
  17.         </TextView>    
  18.     <Button      
  19.         android:id="@+id/Button01"      
  20.         android:layout_width="wrap_content"      
  21.         android:layout_height="wrap_content" android:text="编辑">    
  22.     </Button>    
  23. </LinearLayout>    
  24.   
  25. 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenghome/archive/2010/06/19/5680457.aspx  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值