logo小熊博客
首页 代码速查表 fk标记语言示例 登录
目录
设置android弹窗
private void showNoticeDialog() {
        // 获取LayoutInflater实例
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        // 使用LayoutInflater加载弹窗布局
        View popupView = inflater.inflate(R.layout.notices, null);

        // 获取屏幕密度
        float density = getResources().getDisplayMetrics().density;

        // 设置宽度和高度为300dp
        int widthInPx  = Math.round(500 * density); // 300 dp 转换为像素
        int heightInPx = Math.round(300 * density); // 300 dp 转换为像素

        // 创建PopupWindow实例并设置宽度和高度
        final PopupWindow popupWindow = new PopupWindow(popupView,
                widthInPx, // 宽度 (像素)
                heightInPx, // 高度 (像素)
                true); // 设置弹窗可聚焦

        // 设置点击外部区域关闭弹窗
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        // 显示弹窗
        View parentView = findViewById(android.R.id.content); // 替换为你需要的父视图
        popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);

        ImageView closeButton = popupView.findViewById(R.id.back);

        closeButton.setOnClickListener(v -> popupWindow.dismiss());

        // 获取TextView并设置点击事件
        TextView title1 = popupView.findViewById(R.id.titl1);
        TextView title2 = popupView.findViewById(R.id.titl2);
        TextView title3 = popupView.findViewById(R.id.titl3);
        TextView title4 = popupView.findViewById(R.id.titl4);

        title1.setOnClickListener(v -> handleTextViewClick(1));
        title2.setOnClickListener(v -> handleTextViewClick(2));
        title3.setOnClickListener(v -> handleTextViewClick(3));
        title4.setOnClickListener(v -> handleTextViewClick(4));

    }
// 点击事件处理方法
    private void handleTextViewClick(int index) {
        switch (index) {
            case 1:
                // 处理第一个TextView的点击事件
                Toast.makeText(this, "Title 1 clicked", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                // 处理第二个TextView的点击事件
                Toast.makeText(this, "Title 2 clicked", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                // 处理第三个TextView的点击事件
                Toast.makeText(this, "Title 3 clicked", Toast.LENGTH_SHORT).show();
                break;
            case 4:
                // 处理第四个TextView的点击事件
                Toast.makeText(this, "Title 4 clicked", Toast.LENGTH_SHORT).show();
                break;
        }
    }
上一篇:android设置横屏
下一篇:PopupWindow 显示问题
请我喝奶茶!
赞赏码
手机扫码访问
手机访问