当前位置: 编程技术>移动开发
本页文章导读:
▪在状态栏中增添Activity Indicator View 图标 在状态栏中添加Activity Indicator View 图标
添加此属性即可:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
......
▪ 运用自定义的BaseAdapter实现LIstView的展示 使用自定义的BaseAdapter实现LIstView的展示
[size=medium;]使用自定义的baseadapter实现listview的展示[/size] [size=small;]实现以下功能点:[/size] [size=medium;]1.通过自定义的baseadapter展示listview;[/si.........
▪ 透过字符串构建查找图片资源ID 通过字符串构建查找图片资源ID
为了灵活显示图片,往往会通过设置一些变量来实现,而字符串是比较经常被使用到的 下面介绍2种通过字符串显示图片的方法 1. 不把图片放在res/drawable下,.........
[1]在状态栏中增添Activity Indicator View 图标
来源: 互联网 发布时间: 2014-02-18
在状态栏中添加Activity Indicator View 图标
添加此属性即可:
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[2] 运用自定义的BaseAdapter实现LIstView的展示
来源: 互联网 发布时间: 2014-02-18
使用自定义的BaseAdapter实现LIstView的展示
[size=medium;]使用自定义的baseadapter实现listview的展示[/size]
[size=small;]实现以下功能点:[/size]
[size=medium;]1.通过自定义的baseadapter展示listview;[/size]
[size=medium;]2.动态修改、刷新listview内容;[/size]
[size=medium;]3.在自定义的baseadapter中实现listview内容的点击和长按点击事件;[/size]
[size=medium;]4.在baseadapter的getview方法中动态增加新组件;[/size]
[size=small;]效果图如下:[/size]
<span >xml配置文件1(list_view01.xml)</span>
<span >xml配置文件2(list_view01_item.xml)</span>
<span >自定义的baseadapter类代码</span>
[size=medium;]使用自定义的baseadapter实现listview的展示[/size]
[size=small;]实现以下功能点:[/size]
[size=medium;]1.通过自定义的baseadapter展示listview;[/size]
[size=medium;]2.动态修改、刷新listview内容;[/size]
[size=medium;]3.在自定义的baseadapter中实现listview内容的点击和长按点击事件;[/size]
[size=medium;]4.在baseadapter的getview方法中动态增加新组件;[/size]
[size=small;]效果图如下:[/size]
<span >xml配置文件1(list_view01.xml)</span>
<?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" > <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <edittext android:id="@+id/edittext01edit" android:layout_width="110sp" android:layout_height="wrap_content" android:text="" /> <button android:id="@+id/button01edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动态修改第1行第2列数据" android:onclick="editpersonandrefreshlistview01" /> </linearlayout> <listview android:id="@+id/listview01" android:layout_width="fill_parent" android:layout_height="fill_parent" /></linearlayout>
<span >xml配置文件2(list_view01_item.xml)</span>
<?xml version="1.0" encoding="utf-8"?><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" ><textview android:id="@+id/listview01item01" android:layout_width="50sp" android:layout_height="wrap_content" /><textview android:id="@+id/listview01item02" android:layout_width="90sp" android:layout_height="wrap_content" /> <textview android:id="@+id/listview01item03" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout><span >activity类代码:</span>
package com.zhouzijing.android;import java.util.arraylist;import java.util.list;import com.zhouzijing.android.util.menuentity;import android.app.activity;import android.os.bundle;import android.view.contextmenu;import android.view.contextmenu.contextmenuinfo;import android.view.menuitem;import android.view.view;import android.view.view.oncreatecontextmenulistener;import android.widget.adapterview;import android.widget.adapterview.adaptercontextmenuinfo;import android.widget.adapterview.onitemclicklistener;import android.widget.adapterview.onitemlongclicklistener;import android.widget.adapterview.onitemselectedlistener;import android.widget.baseadapter;import android.widget.edittext;import android.widget.listadapter;import android.widget.listview;import android.widget.toast;public class listviewdemo extends activity { //定义person list<person> persons ; listadapter adapter = null; listview listview01 = null; edittext edittextpersonname = null; //定义上下文菜单 final private static int context_menu_edit = 0;//菜单[编辑]的编号 final private static int context_menu_share = 1;//菜单[分享]的编号 final private static int context_menu_delete = 2;//菜单[删除]的编号 final private static int context_menu_cancel = 3;//菜单[取消]的编号 private menuentity[] contextmenus = { new menuentity(0, context_menu_edit, 0, "编辑"), new menuentity(0, context_menu_share, 1, "分享"), new menuentity(0, context_menu_delete, 2, "删除"), new menuentity(0, context_menu_cancel, 3, "取消"), }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list_view01); //获取组件 listview01 = (listview)findviewbyid(r.id.listview01); edittextpersonname = (edittext)findviewbyid(r.id.edittext01edit); //显示listview initlistallpersons(); showbymybaseadapter(); //初始化listview的事件 //initlistview01event(); } public void initlistallpersons(){ persons = new arraylist<person>(); persons.add(new person(100,"李小龙","香港",android.r.drawable.ic_delete)); persons.add(new person(101,"施瓦辛格","美国",android.r.drawable.ic_dialog_alert)); persons.add(new person(102,"戴安娜王妃","英国",android.r.drawable.ic_dialog_dialer)); persons.add(new person(103,"成龙","香港",android.r.drawable.ic_dialog_map)); persons.add(new person(104,"史泰龙","美国",android.r.drawable.ic_lock_idle_alarm)); persons.add(new person(105,"圣女贞德","法国",android.r.drawable.ic_media_next)); persons.add(new person(106,"列宁","苏联",android.r.drawable.ic_media_play)); persons.add(new person(107,"爱迪生","美国",android.r.drawable.ic_menu_add)); persons.add(new person(108,"孔子","中国",android.r.drawable.ic_media_rew)); persons.add(new person(109,"杨七郎","宋朝",android.r.drawable.ic_menu_zoom)); persons.add(new person(110,"秦始皇","秦国",android.r.drawable.ic_menu_agenda)); persons.add(new person(111,"岳飞","宋朝",android.r.drawable.ic_lock_lock)); } public void showbymybaseadapter(){ adapter = new mybaseadapter(this, persons); listview01.setadapter(adapter); } /** * 按钮button01edit的onclick事件. * @param view */ public void editpersonandrefreshlistview01(view view){ //获取textedit数据 string value = edittextpersonname.gettext().tostring(); //更新listview的内容,并且动态刷新. persons.get(0).name=value; ((baseadapter) adapter).notifydatasetchanged(); } /** * 初始化listview01的事件. */ public void initlistview01event(){ //listview的item点击事件 listview01.setonitemclicklistener(new onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast.maketext(getapplicationcontext(), "[onitemclicklistener]点击了:"+persons.get(position).name, toast.length_short).show(); } }); //listview的item长按点击事件 listview01.setonitemlongclicklistener(new onitemlongclicklistener(){ @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { toast.maketext(getapplicationcontext(), "[onitemlongclicklistener]点击了:"+persons.get(position).name, toast.length_short).show(); return false; } }); //listview的键盘选中事件(直接触摸屏幕选中不会激发) listview01.setonitemselectedlistener(new onitemselectedlistener(){ @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { toast.maketext(getapplicationcontext(), "[onitemselectedlistener:onitemselected]点击了:"+persons.get(position).name, toast.length_short).show(); } @override public void onnothingselected(adapterview<?> parent) { toast.maketext(getapplicationcontext(), "[onitemselectedlistener:onnothingselected]点击了", toast.length_short).show(); } }); //listview组件:创建上下文菜单 listview01.setoncreatecontextmenulistener(new oncreatecontextmenulistener(){ @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { //获取上下文菜单点击的listview的位置(position)属性 adaptercontextmenuinfo info = (adaptercontextmenuinfo) menuinfo; int position = info.position; //设置上下文菜单标题 menu.setheadertitle(persons.get(position).name); //设置上下文菜单栏目 menuentity menuentity = null; for(int i=0;i<contextmenus.length;i++){ menuentity = contextmenus[i]; menu.add(menuentity.getgroupid(),menuentity.getitemid(),menuentity.getorder(),menuentity.gettitle()); } } }); } //监听选择上下文菜单的选择事件 @override public boolean oncontextitemselected(menuitem item) { //选中的上下文菜单的itemid int itemid = item.getitemid(); //获取上下文菜单点击的listview的位置(position)属性 adaptercontextmenuinfo info = (adaptercontextmenuinfo) item.getmenuinfo(); int position = info.position; //toast toast.maketext(getapplicationcontext(), "[oncontextitemselected]点击了"+persons.get(position).name+"" + ",菜单="+contextmenus[itemid].gettitle(), toast.length_short).show(); return true; } }<span >person类代码</span>
package com.zhouzijing.android;public class person { public int id; public string name; public string address; public int photo; public person(int id, string name, string address,int photo) { super(); this.id = id; this.name = name; this.address = address; this.photo = photo; }}
<span >自定义的baseadapter类代码</span>
package com.zhouzijing.android;import java.util.list;import android.content.context;import android.util.log;import android.view.gravity;import android.view.layoutinflater;import android.view.view;import android.view.view.onclicklistener;import android.view.view.onlongclicklistener;import android.view.viewgroup;import android.widget.baseadapter;import android.widget.imageview;import android.widget.linearlayout;import android.widget.textview;import android.widget.toast;public class mybaseadapter extends baseadapter { private list<person> persons; context context; public mybaseadapter(context context,list<person> persons){ this.persons = persons; this.context = context; } @override public int getcount() { return (persons==null)?0:persons.size(); } @override public object getitem(int position) { return persons.get(position); } @override public long getitemid(int position) { return position; } public class viewholder{ textview textviewitem01; textview textviewitem02; textview textviewitem03; imageview imageview; } @override public view getview(int position, view convertview, viewgroup parent) { final person person = (person)getitem(position); viewholder viewholder = null; if(convertview==null){ log.d("mybaseadapter", "新建convertview,position="+position); convertview = layoutinflater.from(context).inflate( r.layout.list_view01_item, null); viewholder = new viewholder(); viewholder.textviewitem01 = (textview)convertview.findviewbyid( r.id.listview01item01); viewholder.textviewitem02 = (textview)convertview.findviewbyid( r.id.listview01item02); viewholder.textviewitem03 = (textview)convertview.findviewbyid( r.id.listview01item03); //动态增加1个imageview viewholder.imageview = new imageview(context); linearlayout.layoutparams mparams = new linearlayout.layoutparams( linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content); mparams.gravity = gravity.center; mparams.width=50; viewholder.imageview.setlayoutparams(mparams); //这个imageview放到listview的第2列之后 ((linearlayout)convertview).addview(viewholder.imageview,2); convertview.settag(viewholder); }else{ viewholder = (viewholder)convertview.gettag(); log.d("mybaseadapter", "旧的convertview,position="+position); } viewholder.textviewitem01.settext(string.valueof(person.id)); viewholder.textviewitem02.settext(person.name); viewholder.textviewitem03.settext(person.address); viewholder.imageview.setimageresource(person.photo); //对listview中第1个textview配置onclick事件 viewholder.textviewitem01.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { toast.maketext(context, "[textviewitem01.setonclicklistener]点击了"+person.name, toast.length_short).show(); } }); //对listview中的每一行信息配置onclick事件 convertview.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { toast.maketext(context, "[convertview.setonclicklistener]点击了"+person.name, toast.length_short).show(); } }); //对listview中的每一行信息配置onlongclick事件 convertview.setonlongclicklistener(new onlongclicklistener(){ @override public boolean onlongclick(view v) { toast.maketext(context, "[convertview.setonlongclicklistener]点击了"+person.name, toast.length_short).show(); return true; } }); return convertview; }}<span >menuentity类代码:</span>
package com.zhouzijing.android.util;/** * 定义菜单实体结构. * @author stephen * */public class menuentity{ /** * 生成菜单. * @param groupid * @param itemid * @param order * @param title */ public menuentity(int groupid, int itemid, int order, charsequence title) { super(); this.groupid = groupid; this.itemid = itemid; this.order = order; this.title = title; } /** * 组. */ private int groupid; /** * 菜单编号. */ private int itemid; /** * 显示顺序号. */ private int order; /** * 菜单显示名称. */ private charsequence title; /** * @return the groupid */ public int getgroupid() { return groupid; } /** * @param groupid the groupid to set */ public void setgroupid(int groupid) { this.groupid = groupid; } /** * @return the itemid */ public int getitemid() { return itemid; } /** * @param itemid the itemid to set */ public void setitemid(int itemid) { this.itemid = itemid; } /** * @return the order */ public int getorder() { return order; } /** * @param order the order to set */ public void setorder(int order) { this.order = order; } /** * @return the title */ public charsequence gettitle() { return title; } /** * @param title the title to set */ public void settitle(charsequence title) { this.title = title; } }
[3] 透过字符串构建查找图片资源ID
来源: 互联网 发布时间: 2014-02-18
通过字符串构建查找图片资源ID
为了灵活显示图片,往往会通过设置一些变量来实现,而字符串是比较经常被使用到的
下面介绍2种通过字符串显示图片的方法
1. 不把图片放在res/drawable下,而是存放在src某个package中(如:com.drawable.resource),这种情况下的调用方法为:
String path = "com/drawable/resource/image.png";
InputStream is = getClassLoader().getResourceAsStream(path);
Drawable.createFromStream(is, "src");
2. 如果还是希望直接使用res/drawable中的图片,就需要通过下面的方法了:
假设创建工程的时候,填写的package名字为:com.test.image
int resID = getResources().getIdentifier("goto_radar", "drawable", "com.test.image");
Drawable image = getResources().getDrawable(resID);
当然哪种方法好,就仁者见仁智者见智了。大家按需吧。
最新技术文章: