【问题标题】:Programatically change check box state in dialog box?以编程方式更改对话框中的复选框状态?
【发布时间】:2014-09-25 09:33:47
【问题描述】:

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

How can I programatically change the state of the checkboxes so that when 'Select All' is checked, all the others are also checked, and when one of the others is selected, 'Select All' is unchecked?我可以在配置中设置初始状态,但不知道如何动态更改框的选中状态。

谢谢。

【问题讨论】:

  • 您可以在 onclickListener() 上设置检查,您可以在其中放置所有 id setChecked(true)。
  • 但复选框位于警报对话框中,因此它们没有 ID。

标签: java android checkbox android-alertdialog


【解决方案1】:

但复选框位于警报对话框中,因此它们没有 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

【讨论】:

    【解决方案2】:

    使用 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;
            }
    

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

    否则在 listview 中监听检查选项。如果出现任何问题,请尝试让我知道

    【讨论】:

    • 谢谢。不过,这似乎是一种非常复杂的实现方式。
    • 什么都没有,只要使用它,你就会发现它不是。
    猜你喜欢
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2016-03-24
    相关资源
    最近更新 更多