android长按弹出对话框
16lz
2021-01-26
//注意,listview中的子布局一般优先获得焦点,尤其是在子布局里含有button控件时候,button自动抢占焦点。此时对listview的长按弹出对话框应该放在适配器里对item监听
对一个相对布局长按弹出对话框
rl.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View view , ContextMenuInfo info) {
menu.add(0, 0, 0, R.string.deleteRecordItem);
menu.add(0, 1, 0, R.string.clearAllRecod);
/* Add as many context-menu-options as you want to. */
}
});
点击之后的响应方法
@Override
public boolean onContextItemSelected(MenuItem aItem) {ContextMenuInfo menuInfo = (ContextMenuInfo) aItem.getMenuInfo();
/* Switch on the ID of the item, to get what the user selected. */
switch (aItem.getItemId()) {
case 0:
deleteFile(list.get(posItem));
list.remove(posItem);
adapter.notifyDataSetChanged();
break;
case 1:
for (int i = 0; i < list.size(); i++) {
deleteFile(list.get(i));
}
list.clear();;
adapter.notifyDataSetChanged();
break;
default:
break;
}
return false;
}
更多相关文章
- 安卓开发06:布局-线性布局 LinearLayout
- Android(安卓)对话框AlertDialog和AlertDialog.Builder两者的区
- 【Android开发】布局管理器-相对布局
- ViewPager And Fragment
- 将Android(安卓)Studio默认布局ConstraintLayout切换成LinearLay
- android 纯代码 详细编写布局文件
- Android中的RecyclerView源码分析
- android实现点击按钮切换不同的fragment布局
- 让用户不再遭遇ANR