Android 简易文件选择Dialog

本文介绍了一个简单的Android文件选择器应用的实现方式。该应用通过自定义对话框展示设备上的文件和目录,并允许用户进行导航和选择。代码示例包括文件选择活动、对话框布局及适配器逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fileselect.java

package com.example.fileselect;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class fileselect extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fileselect);
		
		Button btn = (Button)this.findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				show();
			}
		});
	}
	
	private void show(){
		fileselectDialog dts = new fileselectDialog(this);
		dts.show();
	}
	
}


fileselectDialog.java

package com.example.fileselect;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class fileselectDialog extends Dialog {
	
	private ArrayList<HashMap<String,String>> CurrentDirOrFileList;
	private Handler MainHandler;
	private mAdapter myAdapter;
	private String currentPath;
	private Context mContext;
	
	private TextView currentpathTextView;

	public fileselectDialog(Context context) {
		super(context);
		mContext = context;
		CurrentDirOrFileList = new ArrayList<HashMap<String,String>>();
		myAdapter = new mAdapter();
		currentPath = Environment.getExternalStorageDirectory().getAbsolutePath();
	}
	
	class mAdapter extends BaseAdapter {
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			convertView = LayoutInflater.from(mContext).inflate(R.layout.sub,null);
			
			ImageView iv = (ImageView)convertView.findViewById(R.id.imageView1);
			TextView tv = (TextView)convertView.findViewById(R.id.textView1);
			
			HashMap<String,String> File = CurrentDirOrFileList.get(position);
			if(File.get("type").equals("D"))
				iv.setImageResource(R.drawable.dir);
			if(File.get("type").equals("F"))
				iv.setImageResource(R.drawable.file);
			
			tv.setText(File.get("name"));
			
			return convertView;
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public Object getItem(int position) {
			return null;
		}
		
		@Override
		public int getCount() {
			return CurrentDirOrFileList.size();
		}
		
	}
	
	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.fileselectdialog);
		
		currentpathTextView = (TextView)findViewById(R.id.textView1);
		ListView lv = (ListView)findViewById(R.id.listView1);
		lv.setAdapter(myAdapter);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				String path = CurrentDirOrFileList.get(arg2).get("path");
				
				File f = new File(path);
				if(f.isDirectory()){
					currentPath = path;
					getFiles();
				}
			}
		});
		
		ImageButton BackBtn = (ImageButton)findViewById(R.id.imageButton1);
		BackBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				File f = new File(currentPath);
				String parentpath = f.getParentFile().getAbsolutePath();
				currentPath = parentpath;
				getFiles();
			}
		});
		
		MainHandler = new Handler(){
			@Override
			public void handleMessage(Message msg){
				if(msg.obj.toString().equals("find")){
					findTheard ft = new findTheard(currentPath);
					ft.start();
				}
				
				if(msg.obj.toString().equals("refresh")){
					currentpathTextView.setText(currentPath);
					myAdapter.notifyDataSetChanged();
				}
			}
		};
		
		getFiles();
		
	}
	
	public void dialogdismiss(){
		this.dismiss();
	}
	
	private class findTheard extends Thread {
		
		private String findPath;
		
		private findTheard(String Path){
			findPath = Path;
		}
		
		@Override
		public void run()
		{
			File f = new File(findPath);
			
			if(!f.isDirectory()){
				return;
			}
			
			CurrentDirOrFileList.clear();
			File[] flist = f.listFiles();
			if(flist != null){
				for(File subfile : flist){
					HashMap<String,String> filemap = new HashMap<String,String>();
					
					if(subfile.isDirectory())
						filemap.put("type", "D");
					if(subfile.isFile())
						filemap.put("type", "F");
					
					String filename = subfile.getName();
					filemap.put("name", filename);
					
					String filepath = subfile.getAbsolutePath();
					filemap.put("path", filepath);
					
					CurrentDirOrFileList.add(filemap);
				}
			}
			
			Message msg = MainHandler.obtainMessage();
			msg.obj = "refresh";
			MainHandler.sendMessage(msg);
		}
	}
	
	private void getFiles(){
		Message msg = MainHandler.obtainMessage();
		msg.obj = "find";
		MainHandler.sendMessage(msg);
	}
}

fileselect.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" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="选择文件" />

</LinearLayout>

fiileselectdialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="3dp" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:src="@drawable/back" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp"
            android:gravity="center"
            android:text="TextView"
            android:textSize="14dp" />

    </LinearLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="3dp"
        android:paddingBottom="3dp"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:paddingTop="3dp" >

    </ListView>

</LinearLayout>

sub.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="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="4dp"
        android:gravity="center_vertical"
        android:text="TextView"
        android:textSize="10dp" />

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值