简体   繁体   中英

Set onClickListener to button in bottom sheet layout

I want to add onClickListener to a button in the bottom sheet layout dialog. But it is not working. Nothing happens when i click on the button.

CODE

    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();
        }
    });

You need to bind the button by id .

Like

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

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

Likewise And then you can go with click.

If you are following beehive tutorial , then you just need to inflate that layout and then call button through that view.

View view = getLayoutInflater().inflate(R.layout.fragment_filters, null);
                BottomSheetDialog dialog = new BottomSheetDialog(getActivity());
                dialog.setContentView(view);
                dialog.show();
                final Button button = view.findViewById(R.id.helloworld);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getActivity(), "ok", Toast.LENGTH_LONG).show();
                    }
                });

Notice

final Button button = view.findViewById(R.id.helloworld);

where view equals

View view = getLayoutInflater().inflate(R.layout.fragment_filters, null);

ie the fragment i'm using to display in bottom sheet. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM