您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Android_代码模板
Android总结孙沛林第1页代码模板xml布局TextViewTextViewandroid:id=@+id/tv_colorandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=文本android:textSize=22sp/EditTextEditTextandroid:id=@+id/ed_urlandroid:lines=3android:layout_width=match_parentandroid:layout_height=wrap_content/ButtonButtonandroid:id=@+id/btnandroid:text=卸载android:textColor=#fffandroid:textSize=14spandroid:layout_width=66dpandroid:layout_height=30dpandroid:background=@drawable/btn_selectorandroid:layout_marginRight=5dp/Buttonandroid:id=@+id/btnAndroid总结孙沛林第2页android:text=按钮android:textColor=#000android:textSize=14spandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:onClick=btnClick/publicvoidbtnClick(Viewv){if(v.getId()==R.id.btn){}}ImageViewImageViewandroid:id=@+id/logoandroid:src=@drawable/ic_10android:layout_width=70dpandroid:layout_height=70dp/ListViewListViewandroid:id=@+id/lv_mainandroid:layout_width=match_parentandroid:layout_height=wrap_contentandroid:listSelector=@drawable/item_selector/Manifest清单文件权限!--访问互联网--uses-permissionandroid:name=android.permission.INTERNET/!--写入外置设备--uses-permissionandroid:name=android.permission.WRITE_EXTERNAL_STORAGE/Android总结孙沛林第3页!--可以建立和删除文件与文件夹--uses-permissionandroid:name=android.permission.MOUNT_UNMOUNT_FILESYSTEMS/java代码ToastToast.makeText(this,,Toast.LENGTH_SHORT).show();AdapterpublicclassMyAdapterextendsBaseAdapter{/**数据集合*/ListAppInfolist;/**反射器*/LayoutInflaterinflater;publicMyAdapter(){}/***构造器*@paramcontext上下文*/publicMyAdapter(Contextcontext){inflater=LayoutInflater.from(context);}/***传入数据集合*@paramlist*/publicvoidsetList(ListAppInfolist){this.list=list;}@OverridepublicintgetCount(){return(list==null)?0:list.size();}@OverridepublicObjectgetItem(intposition){Android总结孙沛林第4页returnlist.get(position);}@OverridepubliclonggetItemId(intposition){returnposition;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){ViewHolderholder=null;if(convertView==null){convertView=inflater.inflate(R.layout.item,null);holder=newViewHolder();holder.logo=(ImageView)convertView.findViewById(R.id.logo);holder.title=(TextView)convertView.findViewById(R.id.title);holder.version=(TextView)convertView.findViewById(R.id.version);holder.size=(TextView)convertView.findViewById(R.id.size);holder.btn=(Button)convertView.findViewById(R.id.btn);convertView.setTag(holder);}else{holder=(ViewHolder)convertView.getTag();}AppInfoapp=list.get(position);holder.logo.setImageDrawable(app.icon);holder.title.setText(app.AppName);holder.version.setText(版本:+app.versionName);holder.size.setText(大小:+app.size+M);returnconvertView;}publicstaticclassViewHolder{ImageViewlogo;TextViewtitle;TextViewversion;TextViewsize;Buttonbtn;}}Android总结孙沛林第5页Handler多线程+进度条//1声明进度框对象ProgressDialogpd;//显示一个环形进度框publicvoidshowProgressDialog(){pd=newProgressDialog(MainActivity.this);pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);pd.setTitle(刷新列表);pd.setMessage(请耐心等待);pd.show();}//2声明handler对象,处理子线程结束后,UI主线程的更新Handlerhandler=newHandler(){@OverridepublicvoidhandleMessage(Messagemsg){if(msg.what==1){updateData_sort(currSort);pd.dismiss();//关闭进度框}}};//3.子线程privatevoidupdateData(){//启动新线程,处理耗时操作newThread(){publicvoidrun(){list=Utils.getAppInfos(MainActivity.this);//耗时操作try{Thread.sleep(2000);//模拟耗时操作;真机测试时请注释掉}catch(InterruptedExceptione){e.printStackTrace();}handler.sendEmptyMessage(1);}}.start();showProgressDialog();//显示进度框}Android总结孙沛林第6页保持屏幕常亮//保持屏幕常亮getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);初始化ViewprivatevoidinitView(){tv_info=(TextView)findViewById(R.id.tv_info);}独立方法字符串与时间函数转换应用大小/***返回小数点后2位的字符串*@paramsize*@return*/publicstaticStringgetSize(longsize){returnnewDecimalFormat(0.##).format(size*1.0/(1024*1024));}日期格式转换/**Android总结孙沛林第7页*日期格式转换*@parammillis*@return*/publicstaticStringgetTime(longmillis){Datedate=newDate(millis);SimpleDateFormatsdf=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss);returnsdf.format(date);}系统信息判断第三方应用/***判断第三方应用*@paramappInfo*@return*/publicstaticbooleanisThirdPartyApp(ApplicationInfoappInfo){booleanflag=false;if((appInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0){//非系统的flag=true;}elseif((appInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)!=0){//可更新的系统应用flag=true;}returnflag;};获取应用列表/***实体类*/publicclassAppInfo{Android总结孙沛林第8页/**包名*/publicStringpackageName;/**版本名*/publicStringversionName;/**版本号*/publicintversionCode;/**第一次安装时间*/publiclonginsTime;/**更新时间*/publiclongupdTime;/**程序名*/publicStringappName;/**图标*/publicDrawableicon;/**字节大小*/publiclongbyteSize;/**大小*/publicStringsize;@OverridepublicStringtoString(){return\nAppInfo{+packageName='+packageName+'\''+,versionName='+versionName+'\''+,versionCode=+versionCode+,insTime=+Utils.getTime(insTime)+,updTime=+Utils.getTime(updTime)+,appName='+appName+'\''+,icon=+icon+,byteSize=+byteSize+,size='+size+'\''+'}';}}/***获取应用列表*@paramcontext*@return*/publicstaticListAppInfogetAppList(Contextcontext){//返回值集合ListAppInfolist=newArrayListAppInfo();//实例化包管
本文标题:Android_代码模板
链接地址:https://www.777doc.com/doc-2897193 .html