You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Android BottomSheetDialog动态展示NFC芯片扫描得到的数据

解决方案

你遇到的问题不是BottomSheet静态实现导致的,只是没有对布局内的文本控件做赋值操作,按以下步骤修改即可:

  1. 首先确认你的layout_bottom_sheet.xml布局文件中,用于展示NFC芯片信息的TextView已经设置了唯一id,示例如下(你可以替换为自己实际的id):
<TextView
    android:id="@+id/nfc_chip_info"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
  1. 在你现有的BottomSheet初始化代码中,inflate出bottomSheetView之后、调用show()方法之前,找到对应的TextView,将你已经存储的芯片信息变量赋值给它即可,修改后的完整代码如下:
// 此处替换为你实际存储NFC芯片信息的变量名
String nfcChipContent = "你的芯片存储内容";

final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
        Scan.this, R.style.BottomSheetDialogTheme
);
View bottomSheetView = LayoutInflater.from(getApplicationContext())
        .inflate(
                R.layout.layout_bottom_sheet,
                (LinearLayout)findViewById(R.id.bottomSheetContainer)
        );
// 新增赋值逻辑:找到布局中的文本控件,写入芯片信息
TextView nfcInfoTv = bottomSheetView.findViewById(R.id.nfc_chip_info);
nfcInfoTv.setText(nfcChipContent);

bottomSheetView.findViewById(R.id.addToCloset).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        bottomSheetDialog.dismiss();
    }
});
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetDialog.show();

补充说明

如果每次扫描NFC得到的信息不同,只需要在每次触发弹出BottomSheet的逻辑中,把最新的芯片信息赋值给对应变量后再执行上述初始化代码即可,不需要额外修改其他逻辑。

内容的提问来源于stack exchange,提问作者e5lite

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 02:45:02