1、擷取資源: Resources res = getBaseContext().getResources();
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、避免IME面板遮擋,在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-permission
android: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-permission
android: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次