欢迎来到天天文库
浏览记录
ID:10979517
大小:262.00 KB
页数:10页
时间:2018-07-09
《android之文本阅读器》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、android之文本阅读器显示和阅读sd卡上的所有文本文件截图如下:代码如下:目录结构ListAllFileActivity.javapackagecom.example.txtreader;importjava.io.File;importjava.util.ArrayList;importjava.util.List;importandroid.os.Bundle;importandroid.os.Environment;importandroid.app.Activity;importandroid.content.Intent;importandroid.view.Menu;impor
2、tandroid.view.View;importandroid.widget.ArrayAdapter;importandroid.widget.ListView;importandroid.widget.AdapterView;importandroid.widget.AdapterView.OnItemClickListener;publicclassListAllFileActivityextendsActivity{/*文件列表*/privateListfileNameList;privateBundlebundle;//展示SD卡中的所有文件@Overrideprote
3、ctedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_list_all_file);initializeFile();}privatevoidinitializeFile(){Filepath=Environment.getExternalStorageDirectory();File[]f=path.listFiles();putFile(f);}@OverridepublicbooleanonCreateOptionsMen
4、u(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.activity_list_all_file,menu);returntrue;}//读取SD卡中的所有的文件列表,并放到list中privatevoidputFile(File[]f){fileNameList=newArrayList();for(inti=0;i5、iewlv=(ListView)this.findViewById(R.id.fileList);ArrayAdapteradapter=newArrayAdapter(this,android.R.layout.simple_list_item_1,fileToStrArr(fileNameList));lv.setAdapter(adapter);OnItemClickListenerlistener=newOnItemClickListener(){@OverridepublicvoidonItemClick(AdapterView>parent,Vi6、ewview,intposition,longid){Intentintent=newIntent(ListAllFileActivity.this,ViewFile.class);bundle=newBundle();Filefile=fileNameList.get(position);if(file.isDirectory()){File[]f=file.listFiles();putFile(f);}else{bundle.putString("fileName",file.getAbsolutePath());intent.putExtras(bundle);startActivit7、yForResult(intent,0);}}};lv.setOnItemClickListener(listener);}//检查时文件名或者目录privatebooleanisValid(Filef){if(f.isDirectory()){returntrue;}if(f.getName().endsWith(".txt")){returntrue;}else{returnfalse;}}p
5、iewlv=(ListView)this.findViewById(R.id.fileList);ArrayAdapteradapter=newArrayAdapter(this,android.R.layout.simple_list_item_1,fileToStrArr(fileNameList));lv.setAdapter(adapter);OnItemClickListenerlistener=newOnItemClickListener(){@OverridepublicvoidonItemClick(AdapterView>parent,Vi
6、ewview,intposition,longid){Intentintent=newIntent(ListAllFileActivity.this,ViewFile.class);bundle=newBundle();Filefile=fileNameList.get(position);if(file.isDirectory()){File[]f=file.listFiles();putFile(f);}else{bundle.putString("fileName",file.getAbsolutePath());intent.putExtras(bundle);startActivit
7、yForResult(intent,0);}}};lv.setOnItemClickListener(listener);}//检查时文件名或者目录privatebooleanisValid(Filef){if(f.isDirectory()){returntrue;}if(f.getName().endsWith(".txt")){returntrue;}else{returnfalse;}}p
此文档下载收益归作者所有