Java 是否以编程方式更改对话框中的复选框状态?

Java 是否以编程方式更改对话框中的复选框状态?,java,android,checkbox,android-alertdialog,Java,Android,Checkbox,Android Alertdialog,我有一个带有一系列复选框的警报对话框。第一种是“全选”;其他是用户选项 如何通过编程更改复选框的状态,以便在选中“全选”时,也选中所有其他复选框,并且在选中其中一个复选框时,取消选中“全选”复选框?我可以在配置中设置初始状态,但无法确定如何动态更改复选框的选中状态 谢谢 但复选框位于alertdialog中,因此它们没有id 无论何时为警报对话框展开自定义布局,都可以通过findViewById方法获取对复选框的引用 LayoutInflater inflater = LayoutInflate

我有一个带有一系列复选框的警报对话框。第一种是“全选”;其他是用户选项

如何通过编程更改复选框的状态,以便在选中“全选”时,也选中所有其他复选框,并且在选中其中一个复选框时,取消选中“全选”复选框?我可以在配置中设置初始状态,但无法确定如何动态更改复选框的选中状态

谢谢


但复选框位于alertdialog中,因此它们没有id

无论何时为警报对话框展开自定义布局,都可以通过findViewById方法获取对复选框的引用

LayoutInflater inflater = LayoutInflater.from(MyActivity.this);
View customView = inflater.inflate(...);
CheckBox selectAll = customView.findViewById(R.id.select_all);
// attach listeners after this.  

如果要进行大量复杂的处理,请使用DialogFragment。

使用DialogFragment并制作自己的对话框布局。 通过碎片活动扩展您的活动 因为getSupportFragmentManager仅适用于FragmentActivity

//dialog.setArguments(bundle);  if you want to pass values with bundle then use this
如果要显示来自FragmentActivity的dialof,请使用:

 DialogFragment newFragment = H_Calendar_UserScheduleEditFragment
                .newInstance(b);
newFragment.show(getSupportFragmentManager(), "dialog");
如果要从片段中显示它:

DialogFragment newFragment = H_Calendar_UserScheduleEditFragment
                .newInstance(b);

/** Getting callback from dialog fragment by set target fragment **/


    newFragment.setTargetFragment(H_UserCalanderFragment.this, 0);
newFragment.show(getFragmentManager(), "dialog");




public class H_Calendar_UserScheduleEditFragment extends DialogFragment {

    Bundle b;


public static H_Calendar_UserScheduleEditFragment newInstance(Bundle b) {

    H_Calendar_UserScheduleEditFragment hschedule = new H_Calendar_UserScheduleEditFragment();
    hschedule.setArguments(b);

    return hschedule;
}

use this method if you want some callback in Activity:
Create your own listner and just pass Activity refrence or fragment refrence to to your listner refrence variable. And on completion or cancelation of dialog do whatever you want from your listner to do. 

    // Override the Fragment.onAttach() method to instantiate the
        // NoticeDialogListener
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            // Verify that the host activity implements the callback interface
            try {

                Bundle notificationBundle = getArguments();

                // Instantiate the NoticeDialogListener so we can send events to the
                // host
                //this.mListener = (NoticeDialogListener) activity;
            } catch (ClassCastException e) {
                // The activity doesn't implement the interface, throw exception
                throw new ClassCastException(activity.toString()
                        + " must implement NoticeDialogListener");
            }
        }

Use this if you want callback in Fragment:

    @Override
        public void onAttach(Activity activity) {

            super.onAttach(activity);

            try {

                //this.mListener = (NoticeDialogListener) getTargetFragment();

            } catch (final ClassCastException e) {

                throw new ClassCastException(activity.toString()
                        + " must implement OnCompleteListener");
            }
        }

    @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

//To make your dialg fragment to see on full screen , then incude this



getActivity().getWindow().setLayout(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);

    int style = DialogFragment.STYLE_NO_FRAME;
    int theme = R.style.CustomizeDialog;
    // int theme = android.R.style.Theme_Wallpaper;
    setStyle(style, theme);
    b = getArguments();

}

include this style in your style.xml

    <style name="CustomizeDialog" parent="android:Theme.Holo.Dialog">
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowIsFloating">false</item>
        </style>


Now in oncreate view

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View v = inflater.inflate(
                    R.layout.my_schedule_calander_userupdatedelete, container,
                    false);

            FindId(v);
            SetListner();


            return v;
        }
///这就是从片段或活动创建您自己的对话框的全部过程。 现在,您必须实现检查选择逻辑 在listview中选中复选框,并在顶部单独选中一个复选框。 检查是否选中了顶部检查选中listview的所有复选框


否则,请在listview中侦听checkseletion。如果出现任何问题,请尝试告诉我。

您可以在onclickListener上设置check,您可以将所有id设置为CheckedTrue。但是复选框位于alertdialog中,因此它们没有id。谢谢。这看起来是一个非常复杂的实现方法。没有什么是顺从的,只要使用它,你就会注意到它不是。