Android常用方法总结.docx_第1页
Android常用方法总结.docx_第2页
Android常用方法总结.docx_第3页
Android常用方法总结.docx_第4页
全文预览已结束

下载本文档

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

文档简介

Android-常用方法1、获取资源: Resources res = getBaseContext().getResources();Drawable draw=res.getDrawable(R.drawable.icon); Button btn=(Button)findViewById(R.id.myButton);2、获得数组: String ary = getResources().getStringArray(R.array.ary);3、自动提示框:String arrays=new Stringa,ab,abc,bc,bcde,ee; actalert=(AutoCompleteTextView)findViewById(R.id.actalert); ArrayAdapter adapter=new ArrayAdapter( this, android.R.layout.simple_dropdown_item_1line, arrays); actalert.setAdapter(adapter); mactalert=(MultiAutoCompleteTextView)findViewById(R.id.mactvalert); mactalert.setAdapter(adapter);/ 设置多个值之间的分隔符,此处为逗号 mactalert.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer();4、spinner设置数据源spncolor.setPrompt(请选择); ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.attr, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spncolor.setAdapter(adapter);5、显示当前时间:SimpleDateFormat sdf = new SimpleDateFormat(HH:mm:ss); / 将当前时间显示在TextView组件中 tvTime.setText(当前时间: + sdf.format(new Date();6、代码添加布局文件AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(activity); text.setLayoutParams(lp); text.setTextSize(20); text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); text.setPadding(36, 0, 0, 0); text.setText(s);7、自定义窗口标题栏代码 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);/自定义标题栏 setContentView(R.layout.main); /为标题栏设置一个xml布局 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_custom);custom_title.xml 8、Activity仿Dialog Theme(加图标和标题其实就是Activity的icon和titile)(1)、自定义样式 drawable/filled_box drawable/filled_box:资源文件中新建drawable文件夹,新建filled_box样式xml (2)Manifest.xml中配置Acitivity样式 (3)为Activity设置图标 /设定窗口模式(仿Dialog中的icon,带有一个左图标) requestWindowFeature(Window.FEATURE_LEFT_ICON); setContentView(R.layout.main); /设置图片资源 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON , R.drawable.icon);9、从一个Activity1到另一个Activity2,当在第二个Activity2按返回键不出现Activity1的做法是在打开Activity2的同时关闭Activity1 Intent intent = new Intent(Main.this, fowardwidget.class);startActivity(intent);finish();10、允许TextView的文本值拼接(1)允许在TextView的文本值后添加buffer texttv.setText(tv.getText(),TextView.BufferType.EDITABLE);(2)使用Editable对象添加buffer textEditable text=(Editable)tv.getText();text.append(editable);text.append(n):11、如果历史栈中包含Activity,打开此Activity从栈中放到栈顶层而不是从新打开ActivityIntent intent = new Intent(ReorderFour.this, ReorderTwo.class);intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);startActivity(intent);12、避免输入法面板遮挡,在manifest.xml中activity中设置android:windowSoftInputModeandroid:windowSoftInputMode=stateVisible|adjustResize13、获取当前手机壁纸和设置手机壁纸(wallpaper)(1)获取当前壁纸WallpaperManager wm=WallpaperManager.getInstance(this);Drawable wallpaper=wpm.getDrawable();(2)设置当前壁纸,同时要添加壁纸设置权限imapaper.setDrawingCacheEnabled(true);Drawable drawale=this.getResources().getDrawable(R.drawable.bg);imapaper.setImageDrawable(drawale);wpm.setBitmap(imapaper.getDrawingCache();14、常见通过系统服务得到的实例化对象NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);15、检查网络是否连接 /检查网络是否连接 public boolean checkIntent() ConnectivityManager mannager=(ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info=mannager.getActiveNetworkInfo(); if(info=null | !info.isConnected() return false; if(info.isRoaming() return true; return true; 16、从资源文件中(asset)读取文本文档/获得输入流InputStream in=getAssets().open(read_asset.txt);int size=in.available();/将输入流读到字节数组中(内存)byte buffer=new bytesize;in.read(buffer);in.close();String text=new String(buffer);17、TextView、Button等设置文本滚动(跑马灯效果),控件必须获得焦点才能有滚动效果,并且文字长度大于控件长度 android:singleLine=true android:ellipsize=marquee android:focusable=true android:marqueeRepeatLimit=marquee_forever /marquee_forever:一直滚动下去,n(整数):滚动n次18、TextView文本添加下划线TextView textView = (TextView)findViewById(R.id.testView); textView.setText(Html.fromHtml(+hahaha+);19、TextView添加图片String src=+R.drawable.qq; Html.ImageGetter imageGetter =new ImageGetter() Drawable draw=null; Override public Drawable getDrawable(String source) / TODO Auto-generated method stub int id=Integer.parseInt(source.trim(); draw=getResources().getDrawable(id); draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight(); return draw; ; tvquestion.append(Html.fromHtml(img src=+src+, imageGetter, null);20、秒转成00:00:00 int hour=time/60/60%60; int minute = time/60%60; int second = time%60; String strhour=hour10 ? 0 + hour : + hour; String strminutes=minute10 ? 0 + minute : + minute; String strseconds=second 10 ? 0 + second : + second; tvtimer.setText(strhour+:+strminutes+:+strseconds);21、TextView添加滚动条 22、Activity自动销毁时调用onSaveInstanceState()方法保存配置信息:可多Activity共享 Override public void onSaveInstanceState(Bundle outState) SharedPreferences.Editor editor=getSharedPreferences(sp_timer, 0).edit(); editor.p

温馨提示

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

评论

0/150

提交评论