码迷,mamicode.com
首页 > 移动开发 > 详细

Android简单的文件浏览器,ListActivity的简单用法

时间:2014-07-29 16:47:52      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   java   color   os   文件   


2014-07-29 13:39:09
MainActivity.java
package com.example.sample_4_21;

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

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
    List<String> filelist = null;
    String rootPath = "/";
    String currentpath;
    String parentPath;
    ArrayAdapter<String> adapter;
    ArrayAdapter<String> maAdapter;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.mPath);
        init(rootPath);
    }

    void init(String path) {
        adapter = new ArrayAdapter<String>(this, R.layout.file_row,
                getFileList(path));
        setListAdapter(adapter);
        tv.setText(path);
        currentpath = path;
    }

    List<String> getFileList(String path) {
        File f = new File(path);
        File[] allfile = f.listFiles();
        filelist = new ArrayList<String>();
        parentPath = f.getParent();
        if (parentPath==null) {//如果为根目录,则f.getparent()为null
            parentPath = "/";
        }
        filelist.add("back to..." +parentPath);
        for (File file : allfile) {
            filelist.add(file.getPath());
        }
        return filelist;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        if (position == 0) {
            if (currentpath == rootPath) {
                Toast.makeText(this, "为根目录", Toast.LENGTH_SHORT).show();
            } else {
                init(parentPath);
            }
        } else {
            if (new File(filelist.get(position)).isFile()) {
                Toast.makeText(this, "文件", Toast.LENGTH_SHORT).show();
            } else if (!new File(filelist.get(position)).canRead()) {
                Toast.makeText(this, "不可读", Toast.LENGTH_SHORT).show();
            } else {
                init(filelist.get(position));
            }
        }

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_UP){
            if(currentpath==rootPath){
                new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定退出吗")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        MainActivity.this.finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                })
                .show();
                return true;
            }else{
                init(parentPath);
                return false;
            }    
        }else{
            return false;
        }
    }
}

 

 
   activity_main.xml  
<RelativeLayout 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" tools:context="${packageName}.${activityClass}" > <TextView android:id="@+id/mPath" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:paddingLeft="10dp" android:textColor="#00ff33" /> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_below="@+id/mPath" android:paddingLeft="10dp" android:layout_height="wrap_content" /> </RelativeLayout>
file_row.xml list——item项
<?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="20dp" android:textSize="15sp" android:textColor="#000000" />

 

Android简单的文件浏览器,ListActivity的简单用法,布布扣,bubuko.com

Android简单的文件浏览器,ListActivity的简单用法

标签:android   style   blog   http   java   color   os   文件   

原文地址:http://www.cnblogs.com/mf0819/p/3875319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!