Drawable draw=res.getDrawable(R.drawable.icon);
2、获得数组: String[] ary = getResources().getStringArray(R.array.ary);
3、自动提示框:
代码String[] arrays=new String[]{"a","ab","abc","bc","bcde","ee"};actalert=(AutoCompleteTextView)findViewById(R.id.actalert);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
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<CharSequence> 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);
8、Activity仿Dialog Theme(加图标和标题其实就是Activity的icon和titile)
(1)、自定义样式
<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"><item name="android:windowBackground">@drawable/filled_box</item>
</style>
@drawable/filled_box:资源文件中新建drawable文件夹,新建filled_box样式xml
代码<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#f0600000"/>
<stroke android:width="3dp" color="#ffff8080"/>
<corners android:radius="3dp"/>
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp"/>
</shape>
(2)Manifest.xml中配置Acitivity样式
代码<activity android:name=".Main"android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
(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 text
tv.setText(tv.getText(),TextView.BufferType.EDITABLE);(2)使用Editable对象添加buffer text
Editable text=(Editable)tv.getText();text.append("editable");
text.append("/n"):
11、如果历史栈中包含Activity,打开此Activity从栈中放到栈顶层而不是从新打开Activity
Intent intent =new Intent(ReorderFour.this, ReorderTwo.class);intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
12、避免输入法面板遮挡,在manifest.xml中activity中设置android:windowSoftInputMode
android:windowSoftInputMode="stateVisible|adjustResize"13、获取当前手机壁纸和设置手机壁纸(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());
<uses-permissionandroid:name="android.permission.SET_WALLPAPER"></uses-permission>
14、常见通过系统服务得到的实例化对象
NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);15、检查网络是否连接
代码//检查网络是否连接publicboolean checkIntent(){
ConnectivityManager mannager=(ConnectivityManager)
this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info=mannager.getActiveNetworkInfo();
if(info==null||!info.isConnected()){
returnfalse;
}
if(info.isRoaming()){
returntrue;
}
returntrue;
}
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
16、从资源文件中(asset)读取文本文档
//获得输入流InputStream in=getAssets().open("read_asset.txt");
int size=in.available();
//将输入流读到字节数组中(内存)
byte[] buffer=newbyte[size];
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次