繁体   English   中英

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

[英]Programatically change check box state in dialog box?

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

如何以编程方式更改复选框的状态,以便在选中“全选”时也选中所有其他复选框,而在选择其他一个时未选中“全选”? 我可以在配置中设置初始状态,但无法解决如何动态更改框的选中状态。

谢谢。

但是,这些复选框位于警报对话框中,因此没有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并创建自己的对话框布局。 使用FragmentActivity扩展您的活动,因为getSupportFragmentManager()仅与FragmentActivity一起使用

//dialog.setArguments(bundle);  if you want to pass values with bundle then use this

如果要显示来自FragmentActivity的拨号,请使用:

 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中监听checkeletion。尝试并让我知道是否出现任何问题

暂无
暂无

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

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