編輯:關於 Android 程式設計
Android 平台並沒有提供標準的「打開」與「另存為」對話框。本文將詳細介紹一個非常簡潔的實現方案,對 Android 初學者尤為友好。這裡的對話框都是以全屏 Activity 的形式呈現的。
主要功能包括:
1、訪問 SD 卡的任意目錄
2、遞歸瀏覽文件夾
3、單文件選擇
4、通過硬件返回鍵逐級返回上一層目錄
5、確認文件選擇的 OK 按鈕
activity_open_file.xml
<LinearLayout xmlns:android="<a href="https://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">https://schemas.android.com/apk/res/android"</a> xmlns:tools="<a href="https://schemas.android.com/tools"" rel="nofollow" target="_blank">https://schemas.android.com/tools</a>" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/LvList" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > </ListView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/BtnOK" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="OK" /> <Button android:id="@+id/BtnCancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Cancel" /> </LinearLayout> </LinearLayout>
OpenFileActivity.java
package com.example.androidfiledialogs; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Spinner; import android.widget.Toast; public class OpenFileActivity extends Activity implements OnClickListener, OnItemClickListener { ListView LvList; ArrayList<String> listItems = new ArrayList<String>(); ArrayAdapter<String> adapter; Button BtnOK; Button BtnCancel; String currentPath = null; String selectedFilePath = null; /* Full path, i.e. /mnt/sdcard/folder/file.txt */ String selectedFileName = null; /* File Name Only, i.e file.txt */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_open_file); try { /* Initializing Widgets */ LvList = (ListView) findViewById(R.id.LvList); BtnOK = (Button) findViewById(R.id.BtnOK); BtnCancel = (Button) findViewById(R.id.BtnCancel); /* Initializing Event Handlers */ LvList.setOnItemClickListener(this); BtnOK.setOnClickListener(this); BtnCancel.setOnClickListener(this); // setCurrentPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"); } catch (Exception ex) { Toast.makeText(this, "Error in OpenFileActivity.onCreate: " + ex.getMessage(), Toast.LENGTH_SHORT).show(); } } void setCurrentPath(String path) { ArrayList<String> folders = new ArrayList<String>(); ArrayList<String> files = new ArrayList<String>(); currentPath = path; File allEntries = new File(path).listFiles(); for (int i = 0; i < allEntries.length; i++) { if (allEntries.isDirectory()) { folders.add(allEntries.getName()); } else if (allEntries.isFile()) { files.add(allEntries.getName()); } } Collections.sort(folders, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } }); Collections.sort(files, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } }); listItems.clear(); for (int i = 0; i < folders.size(); i++) { listItems.add(folders.get(i) + "/"); } for (int i = 0; i < files.size(); i++) { listItems.add(files.get(i)); } adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems); adapter.notifyDataSetChanged(); LvList.setAdapter(adapter); } @Override public void onBackPressed() { if (!currentPath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/")) { setCurrentPath(new File(currentPath).getParent() + "/"); } else { super.onBackPressed(); } } @Override public void onClick(View v) { Intent intent; switch (v.getId()) { case R.id.BtnOK: intent = new Intent(); intent.putExtra("fileName", selectedFilePath); intent.putExtra("shortFileName", selectedFileName); setResult(RESULT_OK, intent); this.finish(); break; case R.id.BtnCancel: intent = new Intent(); intent.putExtra("fileName", ""); intent.putExtra("shortFileName", ""); setResult(RESULT_CANCELED, intent); this.finish(); break; } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String entryName = (String)parent.getItemAtPosition(position); if (entryName.endsWith("/")) { setCurrentPath(currentPath + entryName); } else { selectedFilePath = currentPath + entryName; selectedFileName = entryName; this.setTitle(this.getResources().getString(R.string.title_activity_open_file) + "<span>[</span>" + entryName + "]"); } } } activity_save_file.xml
<LinearLayout xmlns:android="<a href="https://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">https://schemas.android.com/apk/res/android"</a> xmlns:tools="<a href="https://schemas.android.com/tools"" rel="nofollow" target="_blank">https://schemas.android.com/tools</a>" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/SFA_LvList" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > </ListView> <EditText android:id="@+id/SFA_TxtFileName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:text="file.txt" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/SFA_BtnOK" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="OK" /> <Button android:id="@+id/SFA_BtnCancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Cancel" /> </LinearLayout> </LinearLayout> </LinearLayout>
SaveFileActivity.java
package com.example.androidfiledialogs; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class SaveFileActivity extends Activity implements OnClickListener, OnItemClickListener { ListView LvList; ArrayList<String> listItems = new ArrayList<String>(); ArrayAdapter<String> adapter; EditText TxtFileName; Button BtnOK; Button BtnCancel; String currentPath = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_save_file); try { /* Initializing Widgets */ LvList = (ListView) findViewById(R.id.SFA_LvList); TxtFileName = (EditText) findViewById(R.id.SFA_TxtFileName); BtnOK = (Button) findViewById(R.id.SFA_BtnOK); BtnCancel = (Button) findViewById(R.id.SFA_BtnCancel); /* Initializing Event Handlers */ LvList.setOnItemClickListener(this); BtnOK.setOnClickListener(this); BtnCancel.setOnClickListener(this); // setCurrentPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"); } catch (Exception ex) { Toast.makeText(this, "Error in SaveFileActivity.onCreate: " + ex.getMessage(), Toast.LENGTH_SHORT).show(); } } void setCurrentPath(String path) { ArrayList<String> folders = new ArrayList<String>(); ArrayList<String> files = new ArrayList<String>(); currentPath = path; File allEntries = new File(path).listFiles(); for (int i = 0; i < allEntries.length; i++) { if (allEntries.isDirectory()) { folders.add(allEntries.getName()); } else if (allEntries.isFile()) { files.add(allEntries.getName()); } } Collections.sort(folders, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } }); Collections.sort(files, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } }); listItems.clear(); for (int i = 0; i < folders.size(); i++) { listItems.add(folders.get(i) + "/"); } for (int i = 0; i < files.size(); i++) { listItems.add(files.get(i)); } adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems); adapter.notifyDataSetChanged(); LvList.setAdapter(adapter); } @Override public void onBackPressed() { if (!currentPath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/")) { setCurrentPath(new File(currentPath).getParent() + "/"); } else { super.onBackPressed(); } } @Override public void onClick(View v) { Intent intent; switch (v.getId()) { case R.id.SFA_BtnOK: intent = new Intent(); intent.putExtra("fileName", currentPath + TxtFileName.getText().toString()); intent.putExtra("shortFileName", TxtFileName.getText().toString()); setResult(RESULT_OK, intent); this.finish(); break; case R.id.SFA_BtnCancel: intent = new Intent(); intent.putExtra("fileName", ""); intent.putExtra("shortFileName", ""); setResult(RESULT_CANCELED, intent); this.finish(); break; } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String entryName = (String)parent.getItemAtPosition(position); if (entryName.endsWith("/")) { setCurrentPath(currentPath + entryName); } else { this.setTitle(this.getResources().getString(R.string.title_activity_open_file) + "<span>[</span>" + entryName + "]"); TxtFileName.setText(entryName); } } } 以上就是本次分享的全部內容,希望對大家的學習有所幫助。
Android 常見 UI 元件之 ListView(一):輕鬆顯示長清單
用 ListView 顯示一長串項目清單步驟一:建立一個名為「BasicView5」的 Android 專案。步驟二:修改 BasicView5.java,改寫後的程式碼
動手玩 Android 屬性動畫(中):ValueAnimator 與 ObjectAnimator
ValueAnimator 的高階用法 上篇提到補間動畫的缺點時說過,補間動畫只能對 View 物件操作,而屬性動畫不再受這個限制,它可以對任意物件進行動畫。還記得之前的
Android 自定義相機:實現手機拍照功能
系統自帶的相機在定製化需求面前往往不夠用,這時候就需要自己調用 Camera API 做一個。本文用一個完整示例演示如何實現取景預覽、自動對焦與拍照保存。 一、布局文件
Android Studio 安裝與更新卡關的完整解法
先前寫過一篇 Android SDK 無法更新的處理方式,那套思路對 Android Studio 同樣有效。這篇把安裝過程中常見的卡點與對策整理成一份可直接照做的清單。