java:
package EX04_21.txt;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.R.string;
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.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class EX04_21 extends ListActivity {
private String rootPath = "/";
private TextView mTextView;
private List<String> items = null;
private List<String> paths = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView)findViewById(R.id.myTextView);
getFilesDir(rootPath);
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
String currentPath = mTextView.getText().toString();
File file = new File(currentPath);
if (!currentPath.equals(rootPath)) {
getFilesDir(file.getParent());
return;
}
super.onBackPressed();
}
private void getFilesDir(String Path) {
// TODO Auto-generated method stub
File file = new File(Path);
if (!file.canRead()) {
Toast.makeText(EX04_21.this, file.getName()+"为不可读目录。", Toast.LENGTH_SHORT).show();
return;
}
else {
mTextView.setText(Path);
items = new ArrayList<String>();
paths = new ArrayList<String>();
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
items.add(f.getName());
paths.add(f.getPath());
}
}
ArrayAdapter<String> array = new ArrayAdapter<String>(EX04_21.this,
R.layout.my_list_style, items);
setListAdapter(array);
}
private void getFilesDirEx(String Path) {
// TODO Auto-generated method stub
mTextView.setText(Path);
items = new ArrayList<String>();
paths = new ArrayList<String>();
File file = new File(Path);
File[] files = file.listFiles();
if (files == null) {
Toast.makeText(EX04_21.this, "鸟蛋", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(paths.get(position));
if (file.isDirectory()) {
getFilesDir(paths.get(position));
} else {
new AlertDialog.Builder(EX04_21.this)
.setIcon(R.drawable.icon)
.setTitle(file.getName()+"属性")
.setMessage(file.getName()+"不是文件夹,是文件。")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
super.onListItemClick(l, v, position, id);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding = "5px"
android:textSize = "18sp"
/>
<ListView
android:id = "@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
my_list_style.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="20px"
android:textSize = "14sp"
/>
本文介绍了一个简单的Android应用程序,用于展示设备上的文件结构。通过使用Java和Android SDK,该应用能够列出指定路径下的所有文件和文件夹,并允许用户通过点击来导航至子目录。此外,还实现了返回上一级目录的功能。
8035

被折叠的 条评论
为什么被折叠?



