非常簡(jiǎn)單的Android打開和保存對(duì)話框功能
在Android上沒有標(biāo)準(zhǔn)的打開和另存為對(duì)話框。在本代碼中,我將詳細(xì)描述一個(gè)非常簡(jiǎn)單的打開和保存對(duì)話框?qū)崿F(xiàn)過程,對(duì)于Android初學(xué)者來說非常有用,對(duì)話框都是全屏活動(dòng)的。
主要功能:
1、訪問任何目錄的SD卡
2、遞歸訪問文件夾
3、單一文件選擇
4、通過按硬件后退按鈕升級(jí)
5、確認(rèn)文件選擇OK按鈕
activity_open_file.xml
<LinearLayout xmlns:android="<a rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a> xmlns:tools="<a rel="nofollow" target="_blank">http://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 rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a> xmlns:tools="<a rel="nofollow" target="_blank">http://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); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 常見的四種對(duì)話框?qū)嵗v解
- Android 對(duì)話框(Dialog)大全詳解及示例代碼
- Android 對(duì)話框 Dialog使用實(shí)例講解
- Android對(duì)話框自定義標(biāo)題 對(duì)話框標(biāo)題美化操作
- 懸浮對(duì)話框Android代碼實(shí)現(xiàn)
- Android Dialog 對(duì)話框詳解及示例代碼
- Android Dialog對(duì)話框用法實(shí)例詳解
- Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果(7)
- Android中制作自定義dialog對(duì)話框的實(shí)例分享
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android 對(duì)話框sweet-alert-dialog
相關(guān)文章
淺談Android Studio 4.1 更新內(nèi)容
這篇文章主要介紹了淺談Android Studio 4.1 更新內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android自定義簡(jiǎn)單的頂部標(biāo)題欄
這篇文章主要為大家詳細(xì)介紹了Android自定義簡(jiǎn)單的頂部標(biāo)題欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android 實(shí)現(xiàn)獲取手機(jī)里面的所有圖片詳解及實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)獲取手機(jī)里面的所有圖片詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例
本篇文章主要介紹了Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03深入學(xué)習(xí)Android?ANR?的原理分析及解決辦法
Android系統(tǒng)中,AMS和WMS會(huì)檢測(cè)App的響應(yīng)時(shí)間,如果App在特定時(shí)間無法相應(yīng)屏幕觸摸或鍵盤輸入時(shí)間,或者特定事件沒有處理完畢,就會(huì)出現(xiàn)ANR。本文將帶領(lǐng)大學(xué)深入學(xué)習(xí)一下ANR的原理及解決辦法,感興趣的同學(xué)可以學(xué)習(xí)一下2021-11-11Android開發(fā) -- setTag的妙用和The key must be an application-specif
本文主要介紹Android開發(fā)setTag的妙用,小編覺得挺實(shí)用的,給大家一個(gè)參考,希望對(duì)大家學(xué)習(xí)有所幫助。2016-06-06Android設(shè)置PreferenceCategory背景顏色的方法
這篇文章主要介紹了Android設(shè)置PreferenceCategory背景顏色的方法,涉及Android設(shè)置背景色的技巧,需要的朋友可以參考下2015-05-05android studio按鈕監(jiān)聽的5種方法實(shí)例詳解
這篇文章主要介紹了android studio按鈕監(jiān)聽的5種方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03