安卓ListView以及GridView用法以及源码课件_第1页
安卓ListView以及GridView用法以及源码课件_第2页
安卓ListView以及GridView用法以及源码课件_第3页
安卓ListView以及GridView用法以及源码课件_第4页
安卓ListView以及GridView用法以及源码课件_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验5.1:package com.example.test5;import android.os.Bundle;import android.R.string;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.wid

2、get.ListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity private static final String strs = new String "first", "second", "third", "fourth", "fifth"/定义一个String数组用来显示ListView的内容Overrideprotecte

3、d void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView list = (ListView)findViewById(R.id.listView1);list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strs);list.setOnItemClickListener(new On

4、ItemClickListener()Overridepublic void onItemClick(AdapterView<?> arg0, View view, int arg2, long id) / TODO Auto-generated method stubString str = (String) (TextView) view).getText();/Toast.makeText(this, text, duration)Toast.makeText(getApplicationContext(),str, Toast.LENGTH_SHORT).show(););

5、Overridepublic boolean onCreateOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;Layout XML:<RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" an

6、droid:layout_height="match_parent" android:paddingBottom="dimen/activity_vertical_margin" android:paddingLeft="dimen/activity_horizontal_margin" android:paddingRight="dimen/activity_horizontal_margin" android:paddingTop="dimen/activity_vertical_margin&quo

7、t; tools:context=".MainActivity" > <TextView android:id="+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="string/hello_world" /> <ListView android:id="+id/listView1" android:l

8、ayout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="+id/textView1" android:layout_below="+id/textView1" android:layout_marginLeft="26dp" android:layout_marginTop="22dp" > </ListView></Relati

9、veLayout>实验5.2:package org.example.test;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ListActivity;import android.os.Bundle;import android.widget.SimpleAdapter;public class test5 extends ListActivity /* Called when the activity i

10、s first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); /setContentView(R.layout.main); SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.item, new String"title","info","img",new intR.id.title,R.

11、,R.id.img); setListAdapter(adapter); private List<Map<String,Object>>getData() List<Map<String,Object>>list = new ArrayList<Map<String,Object>>(); Map<String,Object> map = new HashMap<String,Object>(); map.put("title", "G1");

12、 map.put("info", "google 1"); map.put("img", R.drawable.aomen); list.add(map); map = new HashMap<String,Object>(); map.put("title", "G2"); map.put("info", "google 2"); map.put("img", R.drawable.photo1426); list.add

13、(map); map = new HashMap<String,Object>(); map.put("title", "G3"); map.put("info", "google 3"); map.put("img", R.drawable.photo1450); list.add(map); return list; Layout XmlItem.XML:<?xml version="1.0" encoding="UTF-8"?>

14、;<LinearLayout xmlns:android=" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" ><ImageViewandroid:id="+id/img" android:layout_width="wrap_content" android:layout_height="wrap_

15、content" android:layout_margin="5px"/> <LinerLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="+id/title" android:layout_width="wrap_content&qu

16、ot; android:layout_height="wrap_content" android:textSize="22px"/> <TextView android:id="+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13px"/> </LinerLayout></LinearLa

17、yout>Main.xml:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><ListView android:id="+id/ListView01&

18、quot; android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView></LinearLayout>实验5.3:package com.example.test5_new3;import android.os.Bundle;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.con

19、tent.Context;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.

20、SimpleAdapter;public class MainActivity extends Activity private GridView gridview;private Integer mThumbIds=R.drawable.photo1423,R.drawable.photo1424,R.drawable.photo1426,R.drawable.photo1433,R.drawable.photo1441,R.drawable.photo1450,R.drawable.photo1453,R.drawable.photo1796,R.drawable.photo1803,;O

21、verrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);gridview =(GridView)findViewById(R.id.gridView1);/gridview.setAdapter(new ImageAdapter(this);ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList

22、<HashMap<String, Object>>();/初始化列表数据 for (int i = 0; i < 9; i+) HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemImage", mThumbIdsi);/ 添加图像资源的ID ID和hashMap数组进行绑定 map.put("ItemText", "宫式布局" + String.valueOf(i);/ 按序号做Ite

23、mText lstImageItem.add(map); SimpleAdapter saImageItems = new SimpleAdapter(this, / 没什么解释 lstImageItem,/ 数据来源 R.layout.item,/ night_item的XML实现 / 动态数组与XML:Item对应的子项 new String "ItemImage", "ItemText" , / Item的XML文件里面的一个ImageView01,两个TextView ID new int R.id.ItemImage01, R.id.ItemT

24、ext01 ); / 添加并且显示 gridview.setAdapter(saImageItems); / 添加消息处理 gridview.setOnItemClickListener(new ItemClickListener(); class ItemClickListener implements OnItemClickListener public void onItemClick(AdapterView<?> arg0,/ The AdapterView where the click happened View arg1,/ The view within the A

25、dapterView that was clicked int position,/第几项的意思 The position of the view in the adapter long Item_id/ The row id of the item that was clicked ) / 在本例中arg2=arg3 SuppressWarnings("unchecked") HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(positio

26、n); / 显示所选Item的ItemText setTitle(String) item.get("ItemText"); Overridepublic boolean onCreateOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;Activity.xml:<RelativeLayout xmlns:android

27、=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="dimen/activity_vertical_margin" android:paddingLeft="dimen/activity_horizontal_margin" android:paddingRight="dimen/activity_horizont

28、al_margin" android:paddingTop="dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="+id/but

29、ton1" android:layout_below="+id/button1" android:layout_marginTop="21dp" android:text="Button" /> <Button android:id="+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft=

30、"+id/button2" android:layout_below="+id/button2" android:layout_marginTop="29dp" android:text="Button" /> <Button android:id="+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layo

31、ut_alignLeft="+id/button3" android:layout_below="+id/button3" android:layout_marginTop="20dp" android:text="Button" /> <Button android:id="+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content"

32、 android:layout_alignLeft="+id/button4" android:layout_below="+id/button4" android:layout_marginTop="30dp" android:text="Button" /> <Button android:id="+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_

33、content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="44dp" android:text="Button" /></RelativeLayout>Main.xml:<?xml version="1.0" encoding="UTF-8"?><LinearLayou

34、t xmlns:android=" xmlns:tools=" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="+id/username_view" android:layout_height="wrap_content" android:layout_width

35、="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="请输入用户名:" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="+id/username_edit" android:layout_height=

36、"wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:capitalize="none" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="

37、;+id/password_view" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="请输入密码:" android:textAppearance="?android:attr/textAppearanceMedi

38、um" /> <EditText android:id="+id/password_edit" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:capitalize="none" android:passwo

39、rd="true" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>实验5.4<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="fill_parent" android:layo

40、ut_height="wrap_content" android:orientation="vertical"> <TextView android:id="+id/username_view" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight=&q

41、uot;20dip" android:text="请输入用户名:" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="+id/username_edit" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft=&qu

42、ot;20dip" android:layout_marginRight="20dip" android:capitalize="none" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="+id/password_view" android:layout_height="wrap_content" android:layout_width="

43、wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="请输入密码:" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="+id/password_edit" android:layout_height="wr

44、ap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:capitalize="none" android:password="true" android:textAppearance="?android:attr/textAppearanceMedium" /><

45、/LinearLayout><RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="dimen/activity_vertical_margin" android:paddingLeft="dimen/activity_horizontal_margin" a

46、ndroid:paddingRight="dimen/activity_horizontal_margin" android:paddingTop="dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_con

47、tent" android:layout_alignLeft="+id/button1" android:layout_below="+id/button1" android:layout_marginTop="21dp" android:text="Button" /> <Button android:id="+id/button3" android:layout_width="wrap_content" android:layout_height=&

48、quot;wrap_content" android:layout_alignLeft="+id/button2" android:layout_below="+id/button2" android:layout_marginTop="29dp" android:text="Button" /> <Button android:id="+id/button4" android:layout_width="wrap_content" android:la

49、yout_height="wrap_content" android:layout_alignLeft="+id/button3" android:layout_below="+id/button3" android:layout_marginTop="20dp" android:text="Button" /> <Button android:id="+id/button5" android:layout_width="wrap_content&quo

50、t; android:layout_height="wrap_content" android:layout_alignLeft="+id/button4" android:layout_below="+id/button4" android:layout_marginTop="30dp" android:text="Button" /> <Button android:id="+id/button1" android:layout_width="wra

51、p_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="44dp" android:text="Button" /></RelativeLayout>package com.example.test5_new4;import a

52、ndroid.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.DialogInterface.OnMultiChoiceClickListener;import android.view.LayoutInflater;import android.view.Menu;impo

53、rt android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button bth1 = (Button)findViewById(R.id.button

54、1);bth1.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View arg0) / TODO Auto-generated method stubnew AlertDialog.Builder(MainActivity.this) .setTitle("标题") .setMessage("文本对话框") .setPositiveButton("确定", null) .setNegativeButton("取消"

55、, null) .show(););Button bth2 = (Button)findViewById(R.id.button2);bth2.setOnClickListener(new View.OnClickListener() String items="亚洲","南极洲","北美洲","南美洲","欧洲","非洲","大洋洲"Overridepublic void onClick(View arg0) / TODO Auto-generated

56、method stubnew AlertDialog.Builder(MainActivity.this) .setTitle("七大洲列表") .setItems(items, null) .show(););Button bth3 = (Button)findViewById(R.id.button3);bth3.setOnClickListener(new View.OnClickListener() String items="亚洲","南极洲","北美洲","南美洲","欧洲","非洲","大洋洲"String itemsname;Overridepublic void onClick(View arg0) / TODO Auto-generated method stubnew AlertDialog.Builder(MainActivity.this) .setIcon(R.drawable.globe) .setTitle(&q

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论