Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android 開發常用方法大集合

編輯:進階開發

在 Android 開發的過程中,有許多高頻使用的程式碼片段,反覆查詢既费时又費力。以下就彙整一份實用的常用方法清單,方便隨時查閱。

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"};
AutoCompleteTextView actalert = (AutoCompleteTextView) findViewById(R.id.actalert);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this, android.R.layout.simple_dropdown_item_1line, arrays);
actalert.setAdapter(adapter);

MultiAutoCompleteTextView 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 的效果

首先自訂一個樣式,並搭配一個 drawable 背景:

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowBackground">@drawable/filled_box</item>
</style>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#f0600000"/>
    <stroke android:width="3dp" android:color="#ffff8080"/>
    <corners android:radius="3dp"/>
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp"/>
</shape>

接著在 Manifest 中為 Activity 套用主題,並設定左側圖示:

requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);

9. 跳轉後不返回上一個 Activity

Intent intent = new Intent(Main.this, fowardwidget.class);
startActivity(intent);
finish();

10. 允許 TextView 文字拼接

tv.setText(tv.getText(), TextView.BufferType.EDITABLE);
Editable text = (Editable) tv.getText();
text.append("editable");

11. 將已存在的 Activity 移到堆疊頂端

Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

12. 避免輸入法遮擋

在 Manifest 的 Activity 中設定:

android:windowSoftInputMode="stateVisible|adjustResize"

13. 取得與設定手機壁紙

// 取得當前壁紙
WallpaperManager wm = WallpaperManager.getInstance(this);
Drawable wallpaper = wm.getDrawable();
// 設定壁紙(需添加 SET_WALLPAPER 權限)
imapaper.setDrawingCacheEnabled(true);
Drawable drawable = this.getResources().getDrawable(R.drawable.bg);
imapaper.setImageDrawable(drawable);
wpm.setBitmap(imapaper.getDrawingCache());

14. 透過系統服務取得物件

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

15. 檢查網絡是否連接

public boolean checkIntent() {
    ConnectivityManager manager = (ConnectivityManager)
            this.getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        return false;
    }
    return true;
}

16. 從 asset 讀取文字檔

InputStream in = getAssets().open("read_asset.txt");
int size = in.available();
byte[] buffer = new byte[size];
in.read(buffer);
in.close();
String text = new String(buffer);

17. TextView 文字跑馬燈效果

控制項必須取得焦點,且文字長度大於控制項長度,才會有滾動效果:

android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"

這份清單涵蓋了資源取得、元件設定、Activity 控制、壁紙與網絡處理等常見場景,收藏起來,日後開發時便能省去不少翻查的時間。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved