Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 如何自定義 Intent 選擇界面的標題

編輯:關於 Android 程式設計

當我們用系統的分享或選擇功能跳出「選擇器」時,標題文字是可以自訂的。做法是使用 Intent.createChooser() 方法建立 Intent,並傳入想要的 String 作為標題。

以 wallpaper 選擇框為例,當在 Launcher 工作區的空白處長按時,會彈出 wallpaper 選擇框,其標題為「Choose wallpaper from」:

private void startWallpaper() {
    showWorkspace(true);
    final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
    Intent chooser = Intent.createChooser(pickWallpaper,
            getText(R.string.chooser_wallpaper));
    // NOTE: 若桌布支援,會在選擇器中加入設定選項
    startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
}

其中,R.string.chooser_wallpaper 對應的字串內容就是「Choose wallpaper from」,定義在 Launcher2 的 Strings.xml 中。

關鍵在於 createChooser() 的第二個參數:只要傳入你要顯示的文字(或字串資源),系統選擇器就會以它作為標題。這在做自訂分享、選擇檔案或選擇桌布等功能時非常好用。

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