废话没有,先看效果
第一步:创建mainactivity
import android.app.activity;import android.content.context;import android.content.intent;import android.os.bundle;import android.view.view;import android.widget.button;import android.widget.textview;import com.rdsa.singlelectedfileorfolder.r;import java.util.arraylist;public class mainactivity extends appcompatactivity { public context mcontext; private button lectfile, lectfolder, mlectfile, mlectfolder; private textview result; private activity activity; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.mcontext = this; this.activity = this; tcontentview(r.layout.activity_main); initview(); initdata(); initevent(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == choofileordiractivity.requestcode && resultcode == choofileordiractivity.resultcode) { arraylist<string> respath = data.getstringarraylistextra(choofileordiractivity.resultdata); stringbuffer sb = new stringbuffer(); for(string item : respath) { sb.append(item).append("rn"); } result.ttext(sb.tostring()); } } private void initview() { this.result = super.findviewbyid(r.id.result); this.lectfile = super.findviewbyid(r.id.lectfile); this.lectfolder = super.findviewbyid(r.id.lectfolder); this.mlectfile = super.findviewbyid(r.id.mlectfile); this.mlectfolder = super.findviewbyid(r.id.mlectfolder); } private void initdata() { } private void initevent() { lectfile.tonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(activity, choofileordiractivity.class); intent.putextra(choofileordiractivity.entervar, choofileordiractivity.singlefile); activity.startactivityforresult(intent, choofileordiractivity.requestcode); } }); lectfolder.tonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(activity, choofileordiractivity.class); intent.putextra(choofileordiractivity.entervar, choofileordiractivity.singlefolder); activity.startactivityforresult(intent, choofileordiractivity.requestcode); } }); mlectfile.tonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(activity, choofileordiractivity.class); intent.putextra(choofileordiractivity.entervar, choofileordiractivity.multifile); activity.startactivityforresult(intent, choofileordiractivity.requestcode); } }); mlectfolder.tonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(activity, choofileordiractivity.class); intent.putextra(choofileordiractivity.entervar, choofileordiractivity.multifolder); a夏天的成语ctivity.startactivityforresult(intent, choofileordiractivity.requestcode); } }); }}
第二步:创建mainactivity布局文件activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.rdsa.lectedfileorfolder.mainactivity"> <button android:id="@+id/lectfolder" android:text="文件夹选择(单选)" android:layout_width="match_parent" android:layout_height="wrap_content" /> <button android:id="@+id/lectfile" android:text="文件选择(单选)" android:layout_width="match_parent" android:layout_height="wrap_content" /> <button android:id="@+id/mlectfolder" android:text="文件夹选择(多选)" android:layout_width="match_parent" android:layout_height="wrap_content" /> <button android:id="@+id/mlectfile" android:text="文件选择(多选)" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="wait for result" /></linearlayout>
第三步:创建目录数据存储类fileordirbeans
public class fileordirbeans { string isfile, name, ischeckvisible, ischecked, absolutepath ; public fileordirbeans(string... params) { this.isfile = params[0]; this.name = params[1]; this.ischeckvisible = params[2]; this.ischecked = params[3]; this.absolutepath = params[4]; } public string getisfile() { return isfile; } public void tisfile(string isfile) { this.isfile = isfile; } public string getname() { return name; } public void tname(string name) { this.name = name; } public string getischeckvisible() { return ischeckvisible; } public void tischeckvisible(string ischeckvisible) { this.ischeckvisible = ischeckvisible; } public string getischecked() { return ischecked; } public void tischecked(string ischecked) { this.ischecked = ischecked; } public string getabsolutepath() { return absolutepath; } public void tabsolutepath(string absolutepath) { this.absolutepath = absolutepath; }}
第四步:创建文件浏览choofileordiractivity
import android.manifest;import android.app.activity;import android.content.intent;import android.content.pm.packagemanager;import android.os.build;import android.os.bundle;import android.os.environment;import android.view.keyevent;import android.view.view;import android.widget.button;import android.widget.listview;import android.widget.textview;import com.rdsa.singlelectedfileorfolder.r;import java.io.file;import java.util.arraylist;import java.util.arrays;import java.util.collections;import java.util.comparator;import java.util.list;public class choofileordiractivity extends activity { public static final string entervar = "entervar"; public static final string resultdata = "lected"; public static final int requestcode = 888; public static final int resultcode = 999; public static final int singlefile = 1000; public static final int singlefolder = 2000; public static final int multifile = 3000; public static final int multifolder = 2001; private string rootpath = environment.getexternalstoragedirectory().getabsolutepath(); private static final int requescode = 5; private static final string need_permission = manifest.permission.write_external_storage; private listview listview4file; private textview nodatatip; private button btcho; private list<string> data; private arraylist<fileordirbeans> fileordirlist; private fileadapter fileadapter; private int entervar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); tcontentview(r.layout.ly_choo_file_or_folder); this.entervar = this.getintent().getintextra(entervar, singlefile); this.initview(); this.initdata(); this.initevent(); } private void initdata() { this.listview4file.temptyview(nodatatip); this.data = new arraylist<>(); this.fileordirlist = new arraylist<>(); this.fileadapter = new fileadapter(this, fileordirlist, entervar); this.listview4file.tadapter(fileadapter); if (build.version.sdk_int >= 23) { // 判断系统版本是否大于6.0 if (checklfpermission(need_permission) == packagemanager.permission_granted) { //检查是否有读写权限 loaddatafrompath(rootpath, entervar);//从路径中加载数据 } el { requestpermissions(new string[]{need_permission}, requescode); // 申请权限 } } el { loaddatafrompath(rootpath, entervar);//系统版本小于6.0直接加载数据 } } private void loaddatafrompath(final string mpath, final int type) { this.data.clear(); // data为 listview 中要显示的数据 this.fileordirlist.clear(); new thread() { public void run() { super.run(); data = orderbyname(mpath, type == singlefolder || type == multifolder); fileordirlist = getbeans(data, type); runonuithread(new runnable() { @override public void run() { fileadapter.tdata(data, fileordirlist);//将数据载入适配器当中 } }); } }.start(); } private arraylist<filblou是什么意思eordirbeans> getbeans(list<string> filelist, final int type) { arraylist<fileordirbeans> resultlist = new arraylist<>(); for(string fileitem : filelist) { file file = new file(fileitem); string isfile = string.valueof(file.isfile()); string ischeckvisible = (file.isfile() || type == singlefolder || type == multifolder) ? "true" : "fal";; fileordirbeans bean = new fileordirbeans(isfile, file.getname(), ischeckvisible, "fal", fileitem); resultlist.add(bean); } return resultlist; } private void initview() { listview4file = findviewbyid(r.id.rvfileview); nodatatip = findviewbyid(r.id.nodatatip); } private void initevent() { fileadapter.tevent(new fileadapter.event() { @override public void enternextdir(string path) { rootpath = path; loaddatafrompath(rootpath, entervar); } }); findviewbyid(r.id.btcho).tonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(); intent.putstringarraylistextra(resultdata, fileadapter.getlectdata()); tresult(resultcode, intent); finish(); } }); } @override public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) { super.onrequestpermissionsresult(requestcode, permissions, grantresults); if (requestcode == requescode && permissions[0].equals(need_permission)) { loaddatafrompath(rootpath, entervar); } } @override public boolean onkeydown(int keycode, keyevent event) { if (event.getkeycode() == keyevent.keycode_back) {//点击的是返回键 if (event.getaction() == keyevent.action_down && event.getrepeatcount() == 0) {//按键的按下事件 if (rootpath.trim().equals(environment.getexternalstoragedirectory().getabsolutepath())) { return super.onkeydown(keycode, event); } el { file file = new file(rootpath); if (!file.exists()) { return super.onkeydown(keycode, event); } rootpath = file.getparent(); loaddatafrompath(rootpath, entervar); return fal; } } } return super.onkeydown(keycode, event); } public static arraylist<string> orderbyname(string filepath, boolean excludefile) { arraylist<string> filenamelist = new arraylist<string>(); file file = new file(filepath); file[] files = file.listfiles(); list filelist = arrays.aslist(files); collections.sort(filelist, new comparator<file>() { @override public int compare(file o1, file o2) { if (o1.isdirectory() && o2.isfile()) return -1; if (o1.isfile() && o2.isdirectory()) return 1; return o1.getname().compareto(o2.getname()); } }); for (file itemfile : files) { if(itemfile.getname().startswith(".")) { continue; } el { if(itemfile.isfile() && excludefile) { continue; } filenamelist.add(itemfile.getabsolutepath()); //往集合中添加符合条件的数据 } } return filenamelist; }}
第五步:创惯性是力吗建choofileordiractivity布局文件
ly_choo_file_or_folder.xml
<?xml version="1.0" encoding="utf-8"?><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <relativelayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#f9f9fa"> <textview android:id="@+id/tvtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" /> <button android:id="@+id/btcho" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_alignparentright="true" 孕妇感冒鼻塞 android:layout_centervertical="true" android:layout_marginright="5dp" android:background="@drawable/bg_button" android:text="确定" android:textcolor="#458b74" /> </relativelayout> <listview android:id="@+id/rvfileview" android:layout_width="match_parent" android:layout_height="match_parent" /> <textview android:id="@+id/nodatatip" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="没有数据可用,返回上一层" /></linearlayout>
第六步:创建listview的数据容器类fileada最小的质数是pter
import android.content.context;import android.view.layoutinflater;import android.view.view;import android.view.viewgroup;import android.widget.baadapter;import android.widget.checkbox;import android.widget.compoundbutton;import android.widget.imageview;import android.widget.textview;import com.rdsa.singlelectedfileorfolder.r;import java.util.arraylist;import java.util.list;public class fileadapter extends baadapter { private context context; private event event; private arraylist<string> lectdata; private arraylist<fileordirbeans> fileordirlist; private layoutinflater mlayoutinflater; // 将 item 的 xml 转换成 view private final int entervar; public fileadapter(context context, final arraylist<fileordirbeans> fileordirlist, int entervar) { this.context = context; this.lectdata = new arraylist<>(); this.mlayoutinflater = layoutinflater.from(context); // context 必须是界面所在的 context this.entervar = entervar; this.fileordirlist = fileordirlist; } @override public int getcount() { return this.fileordirlist.size(); } @override // 数据项 public object getitem(int position) { return fileordirlist.get(position); } @override public long getitemid(int position) { return position; } @override // 显示的内容 public view getview(final int position, view convertview, viewgroup viewgroup) { viewholder viewholder; if(convertview == null) { convertview = mlayoutinflater.inflate(r.layout.filechoo_layout_fileitme, null); viewholder = new viewholder(); viewholder.icon = convertview.findviewbyid(r.id.icon); viewholder.filename = convertview.findviewbyid(r.id.filename); viewholder.checkbox = convertview.findviewbyid(r.id.checkbox); convertview.ttag(viewholder); } el { viewholder = (viewholder)convertview.gettag(); } final fileordirbeans bean = fileordirlist.get(position); final string abspath = bean.getabsolutepath(); bean.tischecked("fal"); // 初始化一个boolean值 来判断是否被选中 for (string s : lectdata) { // 遍历选中的集合 if (s.trim().equals(abspath)) { // 如果集合中的子元素与适配其中的路径相同 bean.tischecked("true"); // 判断已被选中 break; //终止循环 } } viewholder.checkbox.toncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton compoundbutton, boolean ischecked) {//设置checkbox的点击事件 if (ischecked) {//选中状态 if (lectdata.contains(abspath)) {//如果集合中包含了该元素则直接返回 if (entervar == choofileordiractivity.singlefile || entervar == choofileordiractivity.singlefolder) { lectdata.clear(); lectdata.add(abspath); } } el {//否则添加 if(entervar == choofileordiractivity.singlefile || entervar == choofileordiractivity.singlefolder) { lectdata.clear(); } lectdata.add(abspath); } } el {//未选中状态 if (lectdata.contains(abspath)) {//如果集合中包含了该元素则移除 lectdata.remove(abspath); } el {//否则 返回 return; } } notifydatatchanged(); } }); viewholder.icon.timagedrawable(boolean.parboolean(bean.getisfile()) ? context.getresources().getdrawable(r.drawable._16_file) : context.getresources().getdrawable(r.drawable._16_folder)); viewholder.filename.ttext(bean.getname()); viewholder.checkbox.tvisibility((boolean.parboolean(bean.ischeckvisible))? view.visible : view.gone); viewholder.checkbox.tchecked(boolean.parboolean(bean.getischecked())); viewholder.filename.tonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if(!boolean.parboolean(bean.getisfile())) { event.enternextdir(fileordirlist.get(position).getabsolutepath()); } } }); return convertview; } static class viewholder { private imageview icon; private textview filename; private checkbox checkbox; } public interface event { void enternextdir(string path);//进入下一个文件夹 } public void tdata(list<string> data, arraylist<fileordirbeans> fileordirlist) { this.fileordirlist.clear(); this.fileordirlist.addall(fileordirlist); notifydatatchanged(); } public void tevent(event event) { this.event = event; } public arraylist<string> getlectdata() { return lectdata; }}
第七步:创建fileadapter每一项的布局文件
filechoo_layout_fileitme.xml
<?xml version="1.0" encoding="utf-8"?><relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <imageview android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginstart="8dp" android:layout_marginend="8dp" android:layout_alignparentstart="true" android:layout_centervertical="true" android:src="@drawable/file" /> <textview android:id="@+id/filename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="8dp" android:layout_margintop="8dp" android:paddingright="50dp" android:layout_torightof="@+id/icon" android:layout_centervertical="true" android:text="textview" /> <checkbox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentend="true" android:layout_centervertical="true" android:layout_marginend="8dp" /></relativelayout>
第八步:androidmanifest 增加对文件操作的授权
<us-permission android:name="android.permission.read_external_storage" /><us-permission android:name="android.permission.write_external_storage" />
第九步:用到的图片资源_16_file.png和_16_folder.png
本文发布于:2023-04-05 05:15:20,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/dabca708d49c7d947c4bfbe46c107895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:android选择文件(安卓系统文件夹详解).doc
本文 PDF 下载地址:android选择文件(安卓系统文件夹详解).pdf
留言与评论(共有 0 条评论) |