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

时间:2014-08-02 07:32:19

标签: java android checkbox alertdialog

我有一个带有一系列复选框的警告对话框。第一个是'全选';其他是用户选择。

如何以编程方式更改复选框的状态,以便在选中“全选”时,还会检查所有其他复选框,当选择其中一个时,将取消选中“全选”?我可以在配置中设置初始状态,但无法解决如何动态更改框的已检查状态。

感谢。

2 个答案:

答案 0 :(得分:0)

  

但是复选框位于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

答案 1 :(得分:0)

使用DialogFragment并制作自己的对话框布局。 使用FragmentActivity扩展您的活动 因为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;
        }

///这是从片段或Activity创建自己的对话框的全部过程。 现在你必须实现检查选择的逻辑 在列表视图中选中复选框,在顶部单独选中一个。 检查是否选中了顶部检查选择列表视图的所有复选框。

否则在listview中听取checkseletion。尝试并告诉我是否有任何问题