android的组件基本都按如下方法生成:
1、生成一个组件对象:通过xml文件或在代码中生成
2、对组件进行设置
3、添加事件监听器
View:
View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。
可以用在多个Button添加一个监听器,每个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。
view plain copy to clipboard print ?
[b]package[/b] fy.test;
[b]import[/b] android.app.Activity;
[b]import[/b] android.os.Bundle;
[b]import[/b] android.view.View;
[b]import[/b] android.widget.Button;
[b]public[/b] [b]class[/b] Main [b]extends[/b] Activity {
/** Called when the activity is first created. */
@Override
[b]public[/b] [b]void[/b] onCreate(Bundle savedInstanceState) {
[b]super[/b].onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.Button01);
Button button2 = (Button) findViewById(R.id.Button02);
Button button3 = (Button) findViewById(R.id.Button03);
Button button4 = (Button) findViewById(R.id.Button04);
MyListener listener = [b]new[/b] MyListener();
button1.setTag(1);
button1.setOnClickListener(listener);
button2.setTag(2);
button2.setOnClickListener(listener);
button3.setTag(3);
button3.setOnClickListener(listener);
button4.setTag(4);
button4.setOnClickListener(listener);
}
[b]public[/b] [b]class[/b] MyListener [b]implements[/b] View.OnClickListener {
@Override
[b]public[/b] [b]void[/b] onClick(View v) {
[b]int[/b] tag = (Integer) v.getTag();
[b]switch[/b] (tag) {
[b]case[/b] 1:
System.out.println("button1 click");
[b]break[/b];
[b]case[/b] 2:
System.out.println("button2 click");
[b]break[/b];
[b]case[/b] 3:
System.out.println("button3 click");
[b]break[/b];
[b]case[/b] 4:
System.out.println("button4 click");
[b]break[/b];
}
}
}
}
main.xml view plain copy to clipboard print ?
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenghome/archive/2010/06/20/5 681338.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(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关联,设置组件事件响应。
view plain copy to clipboard print ?
[b]package[/b] fy.test;
[b]import[/b] java.util.ArrayList;
[b]import[/b] java.util.HashMap;
[b]import[/b] java.util.Map;
[b]import[/b] android.app.Activity;
[b]import[/b] android.content.Context;
[b]import[/b] android.os.Bundle;
[b]import[/b] android.view.LayoutInflater;
[b]import[/b] android.view.View;
[b]import[/b] android.view.ViewGroup;
[b]import[/b] android.view.View.OnClickListener;
[b]import[/b] android.widget.BaseAdapter;
[b]import[/b] android.widget.Button;
[b]import[/b] android.widget.ListView;
[b]import[/b] android.widget.TextView;
[b]public[/b] [b]class[/b] HellowListView [b]extends[/b] Activity {
[b]private[/b] ArrayList> arrayList = [b]new[/b] ArrayList>();
[b]private[/b] Map map;
/** Called when the activity is first created. */
@Override
[b]public[/b] [b]void[/b] onCreate(Bundle savedInstanceState) {
[b]super[/b].onCreate(savedInstanceState);
setContentView(R.layout.main);
initDate();
MyAdapter adapter = [b]new[/b] MyAdapter([b]this[/b]);
ListView list = (ListView) findViewById(R.id.ListView01);
list.setAdapter(adapter);
}
[b]private[/b] [b]void[/b] initDate() {
map = [b]new[/b] HashMap();
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
}
[b]public[/b] [b]class[/b] MyAdapter [b]extends[/b] BaseAdapter {
[b]private[/b] LayoutInflater inflater;
[b]public[/b] MyAdapter(Context c) {
[b]this[/b].inflater = LayoutInflater.from(c);
}
@Override
[b]public[/b] [b]int[/b] getCount() {
[b]return[/b] arrayList.size();
}
@Override
[b]public[/b] Object getItem([b]int[/b] arg0) {
// TODO Auto-generated method stub
[b]return[/b] [b]null[/b];
}
@Override
[b]public[/b] [b]long[/b] getItemId([b]int[/b] arg0) {
// TODO Auto-generated method stub
[b]return[/b] 0;
}
/**
* 设置数据源与行View关联
* 设置行中个组件的事件响应
* 返回设置好的View
*/
@Override
[b]public[/b] View getView([b]int[/b] arg0, View arg1, ViewGroup arg2) {
//取得要显示的行View
View myView = inflater.inflate(R.layout.list_item, [b]null[/b]);
TextView name = (TextView) myView.findViewById(R.id.TextView01);
TextView phoneNum = (TextView) myView.findViewById(R.id.TextView02);
Button button = (Button) myView.findViewById(R.id.Button01);
//让行View的每个组件与数据源相关联
name.setText((String) arrayList.get(arg0).get("姓名"));
phoneNum.setText((String) arrayList.get(arg0).get("电话"));
[b]final[/b] String note = "按下的是" + arg0;
button.setFocusable([b]false[/b]);
//添加事件响应
button.setOnClickListener([b]new[/b] OnClickListener() {
@Override
[b]public[/b] [b]void[/b] onClick(View v) {
// TODO Auto-generated method stub
System.out.println(note);
}
});
[b]return[/b] myView;
}
}
}
main.xml view plain copy to clipboard print ?
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
sime_list.x ml view plain copy to clipboard print ?
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenghome/archive/2010/06/19/5 680457.aspx
1、生成一个组件对象:通过xml文件或在代码中生成
2、对组件进行设置
3、添加事件监听器
View:
View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。
可以用在多个Button添加一个监听器,每个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。
view plain copy to clipboard print ?
[b]package[/b] fy.test;
[b]import[/b] android.app.Activity;
[b]import[/b] android.os.Bundle;
[b]import[/b] android.view.View;
[b]import[/b] android.widget.Button;
[b]public[/b] [b]class[/b] Main [b]extends[/b] Activity {
/** Called when the activity is first created. */
@Override
[b]public[/b] [b]void[/b] onCreate(Bundle savedInstanceState) {
[b]super[/b].onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.Button01);
Button button2 = (Button) findViewById(R.id.Button02);
Button button3 = (Button) findViewById(R.id.Button03);
Button button4 = (Button) findViewById(R.id.Button04);
MyListener listener = [b]new[/b] MyListener();
button1.setTag(1);
button1.setOnClickListener(listener);
button2.setTag(2);
button2.setOnClickListener(listener);
button3.setTag(3);
button3.setOnClickListener(listener);
button4.setTag(4);
button4.setOnClickListener(listener);
}
[b]public[/b] [b]class[/b] MyListener [b]implements[/b] View.OnClickListener {
@Override
[b]public[/b] [b]void[/b] onClick(View v) {
[b]int[/b] tag = (Integer) v.getTag();
[b]switch[/b] (tag) {
[b]case[/b] 1:
System.out.println("button1 click");
[b]break[/b];
[b]case[/b] 2:
System.out.println("button2 click");
[b]break[/b];
[b]case[/b] 3:
System.out.println("button3 click");
[b]break[/b];
[b]case[/b] 4:
System.out.println("button4 click");
[b]break[/b];
}
}
}
}
main.xml view plain copy to clipboard print ?
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenghome/archive/2010/06/20/5 681338.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(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关联,设置组件事件响应。
view plain copy to clipboard print ?
[b]package[/b] fy.test;
[b]import[/b] java.util.ArrayList;
[b]import[/b] java.util.HashMap;
[b]import[/b] java.util.Map;
[b]import[/b] android.app.Activity;
[b]import[/b] android.content.Context;
[b]import[/b] android.os.Bundle;
[b]import[/b] android.view.LayoutInflater;
[b]import[/b] android.view.View;
[b]import[/b] android.view.ViewGroup;
[b]import[/b] android.view.View.OnClickListener;
[b]import[/b] android.widget.BaseAdapter;
[b]import[/b] android.widget.Button;
[b]import[/b] android.widget.ListView;
[b]import[/b] android.widget.TextView;
[b]public[/b] [b]class[/b] HellowListView [b]extends[/b] Activity {
[b]private[/b] ArrayList> arrayList = [b]new[/b] ArrayList>();
[b]private[/b] Map map;
/** Called when the activity is first created. */
@Override
[b]public[/b] [b]void[/b] onCreate(Bundle savedInstanceState) {
[b]super[/b].onCreate(savedInstanceState);
setContentView(R.layout.main);
initDate();
MyAdapter adapter = [b]new[/b] MyAdapter([b]this[/b]);
ListView list = (ListView) findViewById(R.id.ListView01);
list.setAdapter(adapter);
}
[b]private[/b] [b]void[/b] initDate() {
map = [b]new[/b] HashMap();
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
}
[b]public[/b] [b]class[/b] MyAdapter [b]extends[/b] BaseAdapter {
[b]private[/b] LayoutInflater inflater;
[b]public[/b] MyAdapter(Context c) {
[b]this[/b].inflater = LayoutInflater.from(c);
}
@Override
[b]public[/b] [b]int[/b] getCount() {
[b]return[/b] arrayList.size();
}
@Override
[b]public[/b] Object getItem([b]int[/b] arg0) {
// TODO Auto-generated method stub
[b]return[/b] [b]null[/b];
}
@Override
[b]public[/b] [b]long[/b] getItemId([b]int[/b] arg0) {
// TODO Auto-generated method stub
[b]return[/b] 0;
}
/**
* 设置数据源与行View关联
* 设置行中个组件的事件响应
* 返回设置好的View
*/
@Override
[b]public[/b] View getView([b]int[/b] arg0, View arg1, ViewGroup arg2) {
//取得要显示的行View
View myView = inflater.inflate(R.layout.list_item, [b]null[/b]);
TextView name = (TextView) myView.findViewById(R.id.TextView01);
TextView phoneNum = (TextView) myView.findViewById(R.id.TextView02);
Button button = (Button) myView.findViewById(R.id.Button01);
//让行View的每个组件与数据源相关联
name.setText((String) arrayList.get(arg0).get("姓名"));
phoneNum.setText((String) arrayList.get(arg0).get("电话"));
[b]final[/b] String note = "按下的是" + arg0;
button.setFocusable([b]false[/b]);
//添加事件响应
button.setOnClickListener([b]new[/b] OnClickListener() {
@Override
[b]public[/b] [b]void[/b] onClick(View v) {
// TODO Auto-generated method stub
System.out.println(note);
}
});
[b]return[/b] myView;
}
}
}
main.xml view plain copy to clipboard print ?
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
sime_list.x ml view plain copy to clipboard print ?
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
[b][/b]
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenghome/archive/2010/06/19/5 680457.aspx