android——文件目录选择
来源:互联网 发布:手机淘宝论坛怎么找 编辑:程序博客网 时间:2024/06/03 21:31
主活动的布局文件 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/filedire_button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择文件" />
<TextView
android:id="@+id/filedire_textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=" " />
</LinearLayout>
显示图片如下
创建适配器所需的布局文件 adapter.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" >
<ListView
android:id="@+id/adapter_listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/adapter_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.48"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/adapter_filename"
android:layout_width="242dp"
android:layout_height="match_parent"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
主活动程序 MainActivity.java
package com.example.filedirec;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button chooseFile;
private TextView fileDire;
private String curPath = new String("/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chooseFile = (Button)findViewById(R.id.filedire_button1);
fileDire = (TextView)findViewById(R.id.filedire_textView1);
chooseFile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
openCurDir(curPath);
}
});
}
private void openCurDir(String curPath){
File f = new File(curPath);
File[] file = f.listFiles();
final List<Map<String,Object>> listItem = new ArrayList<Map<String,Object>>();
if(!curPath.equals("/")){//如果不是根目录的话就在要显示的列表中加入此项
Map<String,Object> map1=new HashMap<String,Object>();
map1.put("name", "返回上一级目录");
map1.put("image", R.drawable.back02);
map1.put("path",f.getParent());
map1.put("isDire", true);
listItem.add(map1);
}
if(file != null){//必须判断 否则目录为空的时候会报错
for(int i = 0; i < file.length; i++){
Map<String,Object> map=new HashMap<String,Object>();
map.put("name", file[i].getName());
map.put("image", (file[i].isDirectory()) ? R.drawable.folder : R.drawable.doc);
map.put("path",file[i].getPath());
map.put("isDire", file[i].isDirectory());
listItem.add(map);
}
}
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,listItem,R.layout.adapter,
new String[]{"name","image"},new int[]{R.id.adapter_filename,R.id.adapter_image});
final AlertDialog.Builder b =new Builder(MainActivity.this);
b.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
if((Boolean) listItem.get(arg1).get("isDire")){
openCurDir((String)listItem.get(arg1).get("path"));
}else{
fileDire.setText((String)listItem.get(arg1).get("path"));
}
}
});
b.show();
}
}
对话框显示如下
- android——文件目录选择
- 选择目录对话框和选择文件对话框——CFileDialog
- 选择文件、选择目录
- android目录查看,文件选择到文本框
- Android 目录选择获取文件路径
- 选择文件&选择储存目录
- MFC 选择文件目录
- 文件目录选择
- 文件目录选择
- swing选择目录、文件
- java文件\目录选择
- 选择文件存放目录
- 选择文件目录
- SWT 文件 目录选择
- Android文件管理——选择程序打开文件
- 选择目录对话框和选择文件对话框
- 选择目录对话框和选择文件对话框
- [MFC]目录选择与文件选择
- socket编程解决网络数据的传输
- hdu 4750 Count The Pairs
- 对彩色图像进行色彩通道信息的分割
- 跟着鬼哥学Android java hook (一)
- java webservice 用户验证 (服务端 + 客户端)
- android——文件目录选择
- Balloon Comes!
- Devexpress控件之TreeList的初始赋值和勾选值保存
- pdf文件转换器的使用方法和下载地址
- Python 正则表达式验证十六进制数字
- Financial Management
- Http头(转载自博客园)
- Struts2文件上传无刷新,实现进度条
- JAVA之编程比赛