ListView上的Item長按彈出菜單或者對話框是常見的功能之一: java
其實現以下: android
extends ListActivity{ app
在方法: ide
@Override ui
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.localfilebrowser);
registerForContextMenu(this.getListView()); this
增長方法: spa
- @Override
- public void onCreateContextMenu(ContextMenu menu, View view,
- ContextMenuInfo menuInfo) {
- AdapterView.AdapterContextMenuInfo info;
- try {
- info = (AdapterView.AdapterContextMenuInfo) menuInfo;
- } catch (ClassCastException e) {
- Log.e("test", "bad menuInfo", e);
- return;
- }
- currentPath = new File(paths.get(info.position));
- showDialog(UPDATE_FILEPATH);
-
- }
@Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { AdapterView.AdapterContextMenuInfo info; try { info = (AdapterView.AdapterContextMenuInfo) menuInfo; } catch (ClassCastException e) { Log.e("test", "bad menuInfo", e); return; } currentPath = new File(paths.get(info.position)); showDialog(UPDATE_FILEPATH); }
本文使用了彈出式窗口,若是是菜單的話,須要增長以下代碼 .net
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- super.onContextItemSelected(item);
- AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
- .getMenuInfo();
- // Remember current selection
- IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter();
- if (adapter == null) {
- return false;
- }
- return true;
- }
@Override public boolean onContextItemSelected(MenuItem item) { super.onContextItemSelected(item); AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item .getMenuInfo(); // Remember current selection IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter(); if (adapter == null) { return false; } return true; }
showDialog(UPDATE_FILEPATH);對應於以下代碼 blog
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case UPDATE_FILEPATH:
- LayoutInflater inflater = LayoutInflater.from(this);
- View view = inflater.inflate(R.layout.dialog_change_filepath, null);
- final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath);
- final TextView newet = (TextView) view.findViewById(R.id.newfilepath);
- prefs = PreferenceManager.getDefaultSharedPreferences(this);
- oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", ""));
- if(currentPath!=null)
- newet.setText(newet.getText()+currentPath.getAbsolutePath());
- return new AlertDialog.Builder(this).setIcon(
- android.R.drawable.ic_dialog_alert).setTitle(
- R.string.dialog_filepath).setView(view)
- .setPositiveButton(android.R.string.ok,
- new OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- Editor editor = prefs.edit();
- editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath());
- editor.commit();
- ListOSDir.this.finish();
- }
- }).setNegativeButton(android.R.string.cancel,
- new OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- // Cancel should not do anything.
- removeDialog(UPDATE_FILEPATH);
- }
- }).create();
- }
- return null;
- }
@Override protected Dialog onCreateDialog(int id) { switch (id) { case UPDATE_FILEPATH: LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.dialog_change_filepath, null); final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath); final TextView newet = (TextView) view.findViewById(R.id.newfilepath); prefs = PreferenceManager.getDefaultSharedPreferences(this); oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", "")); if(currentPath!=null) newet.setText(newet.getText()+currentPath.getAbsolutePath()); return new AlertDialog.Builder(this).setIcon( android.R.drawable.ic_dialog_alert).setTitle( R.string.dialog_filepath).setView(view) .setPositiveButton(android.R.string.ok, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Editor editor = prefs.edit(); editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath()); editor.commit(); ListOSDir.this.finish(); } }).setNegativeButton(android.R.string.cancel, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Cancel should not do anything. removeDialog(UPDATE_FILEPATH); } }).create(); } return null; }
其中:removeDialog(UPDATE_FILEPATH); 是解決 rem
由於create 一個dialog的開銷很大,因此android會先查找有沒有dialog實例存在,若是有,不create,prepared以後就show,彈出窗口一直都相同的問題
整個程序是選擇一個目錄來設置下載須要存儲的路徑的,代碼以下:
- package cn.cnic.cerc.moblie.clbclient.filemanager;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.ListActivity;
- import android.content.DialogInterface;
- import android.content.SharedPreferences;
- import android.content.DialogInterface.OnClickListener;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.os.Environment;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import android.view.ContextMenu;
- import android.view.LayoutInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.Window;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.AdapterView.AdapterContextMenuInfo;
- import cn.cnic.cerc.moblie.clbclient.R;
-
- public class ListOSDir extends ListActivity {
- private static final int UPDATE_FILEPATH = 0;
- /** Called when the activity is first created. */
- private List<String> items = null;// 存放名稱
- private List<String> paths = null;// 存放路徑
- private String rootPath = "/";
- private TextView tv;
- private SharedPreferences prefs;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- setContentView(R.layout.localfilebrowser);
- registerForContextMenu(this.getListView());
- tv = (TextView) this.findViewById(R.id.TextView);
- this.getFileDir(rootPath);// 獲取rootPath目錄下的文件.
- prefs = PreferenceManager.getDefaultSharedPreferences(this);
- if(prefs.getString("CLB_DOWNLOAD_PATH", "").equals(""))
- {
- File sdcardDir = Environment.getExternalStorageDirectory();
- String path = sdcardDir.getParent() + sdcardDir.getName();
- String fileDirectory = path + java.io.File.separator + "clb";
- Editor editor = prefs.edit();
- editor.putString("CLB_DOWNLOAD_PATH", fileDirectory);
- editor.commit();
- }
- }
- public void getFileDir(String filePath) {
- try {
- this.tv.setText("當前路徑:" + filePath);// 設置當前所在路徑
- items = new ArrayList<String>();
- paths = new ArrayList<String>();
- File f = new File(filePath);
- File[] files = f.listFiles();// 列出全部文件
- // 若是不是根目錄,則列出返回根目錄和上一目錄選項
- if (!filePath.equals(rootPath)) {
- items.add("返回根目錄");
- paths.add(rootPath);
- items.add("返回上一層目錄");
- paths.add(f.getParent());
- }
- // 將全部文件存入list中
- if (files != null) {
- int count = files.length;// 文件個數
- for (int i = 0; i < count; i++) {
- File file = files[i];
- if (file.isDirectory()) {
- items.add(file.getName());
- paths.add(file.getPath());
- }
- }
- }
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, items);
- this.setListAdapter(adapter);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- String path = paths.get(position);
- File file = new File(path);
- // 若是是文件夾就繼續分解
- if (file.isDirectory()) {
- this.getFileDir(path);
- } else {
- new AlertDialog.Builder(this).setTitle("提示").setMessage(
- file.getName() + " 是一個文件!").setPositiveButton("OK",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- }).show();
- }
- }
- private File currentPath;
- @Override
- public void onCreateContextMenu(ContextMenu menu, View view,
- ContextMenuInfo menuInfo) {
- AdapterView.AdapterContextMenuInfo info;
- try {
- info = (AdapterView.AdapterContextMenuInfo) menuInfo;
- } catch (ClassCastException e) {
- Log.e("test", "bad menuInfo", e);
- return;
- }
- currentPath = new File(paths.get(info.position));
- showDialog(UPDATE_FILEPATH);
-
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- super.onContextItemSelected(item);
- AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
- .getMenuInfo();
- // Remember current selection
- IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter();
- if (adapter == null) {
- return false;
- }
- return true;
- }
-
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case UPDATE_FILEPATH:
- LayoutInflater inflater = LayoutInflater.from(this);
- View view = inflater.inflate(R.layout.dialog_change_filepath, null);
- final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath);
- final TextView newet = (TextView) view.findViewById(R.id.newfilepath);
- prefs = PreferenceManager.getDefaultSharedPreferences(this);
- oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", ""));
- if(currentPath!=null)
- newet.setText(newet.getText()+currentPath.getAbsolutePath());
- return new AlertDialog.Builder(this).setIcon(
- android.R.drawable.ic_dialog_alert).setTitle(
- R.string.dialog_filepath).setView(view)
- .setPositiveButton(android.R.string.ok,
- new OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- Editor editor = prefs.edit();
- editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath());
- editor.commit();
- ListOSDir.this.finish();
- }
- }).setNegativeButton(android.R.string.cancel,
- new OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- // Cancel should not do anything.
- removeDialog(UPDATE_FILEPATH);
- }
- }).create();
- }
- return null;
- }
-
-
- }
package cn.cnic.cerc.moblie.clbclient.filemanager; import java.io.File; import java.util.ArrayList; import java.util.List; import android.app.AlertDialog; import android.app.Dialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.DialogInterface.OnClickListener; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.AdapterView.AdapterContextMenuInfo; import cn.cnic.cerc.moblie.clbclient.R; public class ListOSDir extends ListActivity { private static final int UPDATE_FILEPATH = 0; /** Called when the activity is first created. */ private List<String> items = null;// 存放名稱 private List<String> paths = null;// 存放路徑 private String rootPath = "/"; private TextView tv; private SharedPreferences prefs; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.localfilebrowser); registerForContextMenu(this.getListView()); tv = (TextView) this.findViewById(R.id.TextView); this.getFileDir(rootPath);// 獲取rootPath目錄下的文件. prefs = PreferenceManager.getDefaultSharedPreferences(this); if(prefs.getString("CLB_DOWNLOAD_PATH", "").equals("")) { File sdcardDir = Environment.getExternalStorageDirectory(); String path = sdcardDir.getParent() + sdcardDir.getName(); String fileDirectory = path + java.io.File.separator + "clb"; Editor editor = prefs.edit(); editor.putString("CLB_DOWNLOAD_PATH", fileDirectory); editor.commit(); } } public void getFileDir(String filePath) { try { this.tv.setText("當前路徑:" + filePath);// 設置當前所在路徑 items = new ArrayList<String>(); paths = new ArrayList<String>(); File f = new File(filePath); File[] files = f.listFiles();// 列出全部文件 // 若是不是根目錄,則列出返回根目錄和上一目錄選項 if (!filePath.equals(rootPath)) { items.add("返回根目錄"); paths.add(rootPath); items.add("返回上一層目錄"); paths.add(f.getParent()); } // 將全部文件存入list中 if (files != null) { int count = files.length;// 文件個數 for (int i = 0; i < count; i++) { File file = files[i]; if (file.isDirectory()) { items.add(file.getName()); paths.add(file.getPath()); } } } ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); this.setListAdapter(adapter); } catch (Exception ex) { ex.printStackTrace(); } } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); String path = paths.get(position); File file = new File(path); // 若是是文件夾就繼續分解 if (file.isDirectory()) { this.getFileDir(path); } else { new AlertDialog.Builder(this).setTitle("提示").setMessage( file.getName() + " 是一個文件!").setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show(); } } private File currentPath; @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { AdapterView.AdapterContextMenuInfo info; try { info = (AdapterView.AdapterContextMenuInfo) menuInfo; } catch (ClassCastException e) { Log.e("test", "bad menuInfo", e); return; } currentPath = new File(paths.get(info.position)); showDialog(UPDATE_FILEPATH); } @Override public boolean onContextItemSelected(MenuItem item) { super.onContextItemSelected(item); AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item .getMenuInfo(); // Remember current selection IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter(); if (adapter == null) { return false; } return true; } @Override protected Dialog onCreateDialog(int id) { switch (id) { case UPDATE_FILEPATH: LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.dialog_change_filepath, null); final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath); final TextView newet = (TextView) view.findViewById(R.id.newfilepath); prefs = PreferenceManager.getDefaultSharedPreferences(this); oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", "")); if(currentPath!=null) newet.setText(newet.getText()+currentPath.getAbsolutePath()); return new AlertDialog.Builder(this).setIcon( android.R.drawable.ic_dialog_alert).setTitle( R.string.dialog_filepath).setView(view) .setPositiveButton(android.R.string.ok, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { Editor editor = prefs.edit(); editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath()); editor.commit(); ListOSDir.this.finish(); } }).setNegativeButton(android.R.string.cancel, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Cancel should not do anything. removeDialog(UPDATE_FILEPATH); } }).create(); } return null; } }