public void onClink(View view, int position) { //closeLoad(); // 获取列内容 // String str = TextUtils.join(", ", stringList); // 创建弹窗布局 View popupView = getLayoutInflater().inflate(R.layout.showlist, null); // 获取弹窗布局中的控件 TextView titleView = popupView.findViewById(R.id.title); TextView contentView = popupView.findViewById(R.id.content); // 设置弹窗标题和内容 titleView.setText("name"); for (ExcelImportViewModel inventory : iv.inventoryList) { stringList.add(inventory.name + inventory.barCode) ; } contentView.setText(stringList.toString()); // 创建弹窗 PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); // 设置弹窗的背景和动画 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setAnimationStyle(R.style.PopupAnimation); // 显示弹窗 popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0); }点击空白关闭弹窗
时间: 2024-02-14 22:09:02 浏览: 132
计算机试题2.docx
如果你想要在点击弹窗外部的空白区域时关闭弹窗,可以通过设置弹窗的属性来实现。具体来说,你可以设置弹窗的`setOutsideTouchable`和`setFocusable`属性,然后在弹窗外部点击事件的处理方法中关闭弹窗。以下是修改后的代码示例:
```java
public void onClink(View view, int position) {
// 创建弹窗布局
View popupView = getLayoutInflater().inflate(R.layout.showlist, null);
// 获取弹窗布局中的控件
TextView titleView = popupView.findViewById(R.id.title);
TextView contentView = popupView.findViewById(R.id.content);
// 设置弹窗标题和内容
titleView.setText("name");
for (ExcelImportViewModel inventory : iv.inventoryList) {
stringList.add(inventory.name + inventory.barCode);
}
contentView.setText(stringList.toString());
// 创建弹窗
PopupWindow popupWindow = new PopupWindow(popupView,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// 设置弹窗的背景和动画
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setAnimationStyle(R.style.PopupAnimation);
// 设置弹窗的属性
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
// 显示弹窗
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// 处理弹窗外部点击事件,关闭弹窗
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// 执行弹窗关闭时的操作
// 例如,清空列表内容
stringList.clear();
}
});
}
```
上述代码中,`popupWindow.setOutsideTouchable(true)`和`popupWindow.setFocusable(true)`分别设置了弹窗的可点击和可聚焦属性,这样在弹窗外部点击事件的处理方法中才能关闭弹窗。`popupWindow.setOnDismissListener`设置了弹窗关闭时的监听器,当弹窗关闭时可以在该监听器中执行一些操作,例如清空列表内容。
阅读全文