如何在显示自定义对话框后更改视图的可见性 android

How to change visibility of a view in custom dialog after showing it android

想要通过单击某些内容来更改线性布局的可见性 - 在显示对话框后。确定点击方法是正确的。即使没有单击方法也不起作用。有可能吗? 示例:

    singleA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            linearTYT.setVisibility(View.VISIBLE)

                        }else{

linearTYT.setVisibility(View.GONE)
                        }

                    }
});

在对话框代码中。

Use LayoutInflator you might want to override AlertDialog's Button because it will dismiss() by default onClick()

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

    final LinearLayout linearLayout = view.findViewById(R.id.your_linear);
    // Now you can do whatever you want with LinearLayout

    Dialog.OnShowListener showListener = new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {

            View.OnClickListener hideListener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    linearLayout.setVisibility(View.INVISIBLE);

                }
            };

            View.OnClickListener unHideListener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    linearLayout.setVisibility(View.VISIBLE);

                    // Hiding LinearLayout but this doesn't
                    // dismiss the dialog if you want
                    // call dialogInterface.dismiss();
                }
            };

            // Overriding AlertDialog's Button because we don't want it
            // to dismiss in every click
            Button hide = ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE);
            Button unhide = ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_NEGATIVE);
            hide.setOnClickListener(hideListener);
            unhide.setOnClickListener(unHideListener);
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setView(view)
            .setPositiveButton("Hide", null)
            .setNegativeButton("Unhide", null);

    AlertDialog dialog = builder.create();
    dialog.setOnShowListener(showListener);
    dialog.show();