我想将onClickListener添加到底部工作表布局对话框中的按钮。但这不起作用。当我单击按钮时,什么也没有发生。



    button_right = layoutBottomSheet.findViewById(R.id.button_cod);
    button_wrong = layoutBottomSheet.findViewById(R.id.button_paytm);
    layoutBottomSheet = findViewById(R.id.bottom_sheet_layout);
    sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet);



     confirmButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
                sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            } else {
                sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }

        }

    });

    button_right.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(CartActivity.this, "Right", Toast.LENGTH_SHORT).show();
        }
    });

    button_wrong.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(CartActivity.this, "Wrong", Toast.LENGTH_SHORT).show();
        }
    });

最佳答案

您需要通过button绑定id

喜欢

Button confirmButton = (Button) layoutBottomSheet.findViewById(R.id.confirmButton);

Button button_right = (Button) layoutBottomSheet.findViewById(R.id.button);


同样,然后您可以单击。

10-06 10:32