简体   繁体   English

将onClickListener设置为底部工作表布局中的按钮

[英]Set onClickListener to button in bottom sheet layout

I want to add onClickListener to a button in the bottom sheet layout dialog. 我想将onClickListener添加到底部工作表布局对话框中的按钮。 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 . 您需要通过id绑定button

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. 如果您正在遵循蜂巢教程 ,那么您只需膨胀该布局,然后在该视图中调用button。

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); 最后的Button button = view.findViewById(R.id.helloworld);

where view equals 视野等于

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

ie the fragment i'm using to display in bottom sheet. 即我用来显示在底页中的片段。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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