ListView上的Item长按弹出菜单或者对话框是常见的功能之一:
其实现如下:
extends ListActivity{
在方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.localfilebrowser);
registerForContextMenu(this.getListView());
增加方法:
}
本文使用了弹出式窗口,如果是菜单的话,需要增加如下代码:
showDialog(UPDATE_FILEPATH);对应于如下代码:
其中:removeDialog(UPDATE_FILEPATH);
是解决
因为create 一个dialog的开销很大,所以android会先查找有没有dialog实例存在,如果有,不create,prepared之后就show,弹出窗口一直都相同的问题
整个程序是选择一个目录来设置下载需要存储的路径的,代码如下:
}
@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;
}
}