非常简单的Android打开和保存对话框功能
程序员文章站
2024-03-04 18:29:06
在android上没有标准的打开和另存为对话框。在本代码中,我将详细描述一个非常简单的打开和保存对话框实现过程,对于android初学者来说非常有用,对话框都是全屏活动的。...
在android上没有标准的打开和另存为对话框。在本代码中,我将详细描述一个非常简单的打开和保存对话框实现过程,对于android初学者来说非常有用,对话框都是全屏活动的。
主要功能:
1、访问任何目录的sd卡
2、递归访问文件夹
3、单一文件选择
4、通过按硬件后退按钮升级
5、确认文件选择ok按钮
activity_open_file.xml
<linearlayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a> xmlns:tools="<a href="http://schemas.android.com/tools"" 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 href="http://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a> xmlns:tools="<a href="http://schemas.android.com/tools"" 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); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。