AlertDialog.java
package com.leadtone.filelists; import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class AlertDialogs extends Activity {
private Button button;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(openDialog);
}

@Override
protected void onResume() {
openAlertDialog();
super.onResume();
} private OnClickListener openDialog=new OnClickListener(){
@Override
public void onClick(View v) {
openAlertDialog();//点击执行弹出登录对话框
}
};
public ListAdapter getAdapter(){
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ImageManager", R.drawable.donkey);
map.put("ItemManager", "ES文件管理器");
listItem.add(map);
HashMap<String, Object> map1 = new HashMap<String, Object>();
map1.put("ImageManager", R.drawable.icon);
map1.put("ItemManager", "图片");
listItem.add(map1);
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("ImageManager", R.drawable.icon);
map2.put("ItemManager", "音频");
listItem.add(map2);
HashMap<String, Object> map3 = new HashMap<String, Object>();
map3.put("ImageManager", R.drawable.icon);
map3.put("ItemManager", "文档");
listItem.add(map3);

SimpleAdapter listItemAdapter= new SimpleAdapter(this,listItem,R.layout.list_items,
new String[]{"ImageManager","ItemManager"},
new int[]{R.id.ImageManager,R.id.ItemManager});
return listItemAdapter;
}

public void openAlertDialog(){
AlertDialog ad = new AlertDialog.Builder(AlertDialogs.this).setTitle("添加附件").setAdapter(getAdapter(), new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int position) {
onCreateAttachment(position);
} private void onCreateAttachment(int position) {
switch (position) {
case 0:
intent = new Intent(AlertDialogs.this, Filelist.class);
break; default:
Toast.makeText(AlertDialogs.this,"未设", 1).show();
break;
}
if (intent != null) {
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(AlertDialogs.this, R.string.error, 1).show();
}
}
}
}).show();
}

} Filelist.java ?package com.leadtone.filelists;
import java.io.File;
import java.util.ArrayList;
import java.util.List; import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class Filelist extends ListActivity {

private List<String> items = null; @Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.directory_list);
fillWithRoot();
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int selectionRowID = (int) id;
if (selectionRowID == 0) {
fillWithRoot();
} else {
File file = new File(items.get(selectionRowID));
if (file.isDirectory()){
fill(file.listFiles());
}else{
openDialog("您点击的地址是",file.getPath(),"取消");
}
}
} private void fillWithRoot() {
fill(new File("/").listFiles());
} private void fill(File[] files) {
items = new ArrayList<String>();
items.add(getString(R.string.to_top));
for (File file : files){
items.add(file.getPath());
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,
R.layout.file_row, items);
setListAdapter(fileList);
}
private void openDialog(String title, String message, String button) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setNegativeButton(button, null);
builder.show();
}
}
directory_list.xml
?<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <ListView
android:id="@id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_files"/>
</LinearLayout> file_row.xml <?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="wrap_content"/> list_items.xml ?<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" xmlns:android=" http://schemas.android.com/apk/res/android">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:paddingTop="12dip"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImageManager"
/>
<TextView
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_width="fill_parent"
android:id="@+id/ItemManager"
android:gravity="center"
android:layout_marginTop="20sp"
android:layout_toRightOf="@+id/ItemManager" />
</RelativeLayout>
</LinearLayout>

更多相关文章

  1. android开发4:Android布局管理器1(线性布局,相对布局RelativeLayou
  2. android实践--进度对话框(ProgressDialog)
  3. Android 对话框【Dialog】去除白色边框代码
  4. Android UI设计--半透明效果对话框及activity(可做遮罩层)
  5. Android对话框里面的输入值获取不到,空指针异常
  6. Android调用系统自带的文件管理器进行文件选择并获得路径,android
  7. Android的八种对话框的实现
  8. android 展示单选列表对话框 builder.setSingleChoiceItems
  9. Android设置对话框去除黑边

随机推荐

  1. Android学习路线
  2. 做好准备,让你的短信应用迎接Android(安卓
  3. Android生成APK文件
  4. Android官方开发指南翻译(1)-什么是Andro
  5. Android(安卓)HAL 开发 (1)
  6. 在android的webview中实现websocket
  7. android demo之ApiDemos下的text
  8. android打包apk流程
  9. Android(安卓)内存泄漏调试
  10. Android(安卓)开发中使用 SQLite 数据库