您的位置:首页 > 产品设计 > UI/UE

android--UI组件总结

2010-06-21 17:41 211 查看
android的组件基本都按如下方法生成:

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

2、对组件进行设置

3、添加事件监听器

View:

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

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

package fy.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.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 = new 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);
}

public class MyListener implements View.OnClickListener {

@Override
public void onClick(View v) {
int tag = (Integer) v.getTag();
switch (tag) {
case 1:
System.out.println("button1 click");
break;
case 2:
System.out.println("button2 click");
break;
case 3:
System.out.println("button3 click");
break;
case 4:
System.out.println("button4 click");
break;
}
}

}

}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:text="Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Button02"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Button03"
android:id="@+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Button04"
android:id="@+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
本文来自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关联,设置组件事件响应。

package fy.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class HellowListView extends Activity {
private ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
private Map<String, Object> map;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initDate();
MyAdapter adapter = new MyAdapter(this);
ListView list = (ListView) findViewById(R.id.ListView01);
list.setAdapter(adapter);
}

private void initDate() {
map = new HashMap<String, Object>();
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
map.put("姓名", "张三");
map.put("电话", "1132434343333");
arrayList.add(map);
}

public class MyAdapter extends BaseAdapter {
private LayoutInflater inflater;

public MyAdapter(Context c) {
this.inflater = LayoutInflater.from(c);
}

@Override
public int getCount() {
return arrayList.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

/**
* 设置数据源与行View关联
* 设置行中个组件的事件响应
* 返回设置好的View
*/
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
//取得要显示的行View
View myView = inflater.inflate(R.layout.list_item, null);
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("电话"));
final String note = "按下的是" + arg0;
button.setFocusable(false);
//添加事件响应
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println(note);
}
});
return myView;
}
}

}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:text="aaaaaaaa"
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
<ListView
android:id="@+id/ListView01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>


sime_list.x ml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal">
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="编辑">
</Button>
</LinearLayout>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fenghome/archive/2010/06/19/5680457.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: