Android 简易文件选择Dialog

来源:互联网 发布:matlab eig函数源码 编辑:程序博客网 时间:2024/04/29 04:51

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 {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.fileselect);Button btn = (Button)this.findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic 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 {@Overridepublic 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;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic Object getItem(int position) {return null;}@Overridepublic int getCount() {return CurrentDirOrFileList.size();}}@SuppressLint("HandlerLeak")@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic void onClick(View v) {File f = new File(currentPath);String parentpath = f.getParentFile().getAbsolutePath();currentPath = parentpath;getFiles();}});MainHandler = new Handler(){@Overridepublic 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;}@Overridepublic 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>



0 0