【问题标题】:Switch Compat: onCheckedChanged切换兼容:onCheckedChanged
【发布时间】:2018-02-04 14:44:10
【问题描述】:

我正在尝试在我的 Android 应用中实现设置活动,用户可以在其中打开和关闭通知模式。为了实现这一点,我正在使用 Switch Compat。这是布局。这是我的活动布局的一部分。

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:layout_marginStart="24dp"
    android:layout_marginTop="90dp"
    android:layout_marginEnd="16dp">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/notifications"
        android:layout_gravity="start|center"
        android:layout_weight="1"
        android:textSize="20sp"
        android:textStyle="bold"
        android:id="@+id/notifications"/>


    <android.support.v7.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/notificationsSwitch"
        android:layout_gravity="end|center"
        android:theme="@style/SwitchTheme"/>

</LinearLayout>

这是对开关兼容的响应函数。

SwitchCompat notificationsSwitch;
AlertDialog alertDialog;
AlertDialog.Builder builder;

 @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_settings, container, false);
     notificationsSwitch = (SwitchCompat) view.findViewById(R.id.notificationsSwitch);

notificationsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!b) {
                    builder = new AlertDialog.Builder(getActivity());
                    builder.setMessage("Are you sure that you want to turn the Notifications off");
                    builder.setPositiveButton("Turn off ", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            notificationsSwitch.setChecked(false);
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            notificationsSwitch.setChecked(true);
                        }
                    });
                    alertDialog = builder.create();
                    alertDialog.show();
                    alertDialog.setCancelable(false);
                    alertDialog.setCanceledOnTouchOutside(false);
                    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.alertDialogPositiveButton));
                    alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.alertDialogNegativeButton));
                } else {
                    builder = new AlertDialog.Builder(getActivity());
                    builder.setMessage("Are you sure that you want to turn the Notifications on");
                    builder.setPositiveButton("Turn on ", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            notificationsSwitch.setChecked(true);
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            notificationsSwitch.setChecked(false);
                        }
                    });
                    alertDialog = builder.create();
                    alertDialog.show();
                    alertDialog.setCancelable(false);
                    alertDialog.setCanceledOnTouchOutside(false);
                    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.alertDialogPositiveButton));
                    alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.alertDialogNegativeButton));
                }
            }
        });

我想做什么?

  • 正如我所说,这应该让用户能够打开和关闭通知。

怎么了?

  • 代码工作正常,除了一个错误,该错误是当用户想要打开通知时。然后改变主意并在他将循环进入的对话框上单击取消。

为什么会这样?

  • 我已经按照这种方式进行了取消,因此如果用户单击“取消”,则 Switch Compat 会返回到之前的位置。但是由于我有该状态的方法,因此它会触发另一个状态并且用户处于循环中。

我尝试了什么?

  • 我尝试使用 switch,但无法传递布尔变量 b。

  • 我也尝试过使用标志变量,但找不到逻辑。

有人知道我如何克服这个问题吗? 提前致谢。

【问题讨论】:

    标签: android material-design switchcompat


    【解决方案1】:

    您可以使用标志作为类属性,当单击对话框中的选项时将设置为 true,例如 IsFromDialog。

    然后,在对话框的 Onclick 函数中,设置 IsFromDialog = true;

    最后,在 onCheckChanged 开始时,您可以这样做

    如果 (!b && !IsFromDialog) { ... }

    【讨论】:

    • 这就是我所做的,另外我添加了一个 Runnable,如果我想打开它,它将重置标志。非常感谢。
    【解决方案2】:

    OnCheckChanged 侦听器在用户单击开关时触发一次,然后在您的对话框将其更改回来时再次触发,因此是循环。尝试改用 OnTouchListener。

    【讨论】:

      【解决方案3】:

      您可以通过删除OnCheckedChangeListener,然后修改SwitchCompat 的状态,然后重新添加OnCheckedChangeListener 来解决此问题。这样,当您修改开关时,没有任何东西在监听。

      如果你稍微重构一下代码,这将更容易做到。首先,您应该将匿名侦听器提取到片段中的变量。也就是说,删除这个位:

      notificationsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          ...
      });
      

      并添加这个(onCreateView() 之外):

      private CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
          ...
      }
      

      现在你已经完成了,你可以像这样在onCreateView() 中设置它:

      notificationsSwitch = (SwitchCompat) view.findViewById(R.id.notificationsSwitch);
      notificationsSwitch.setOnCheckedChangeListener(listener);
      

      从这里开始,剩下的就很容易了。进入所有四个正面/负面的听众,让他们看起来像这样:

      notificationsSwitch.setOnCheckedChangeListener(null);
      notificationsSwitch.setChecked(true);
      notificationsSwitch.setOnCheckedChangeListener(listener);
      

      【讨论】:

        【解决方案4】:

        实现相同功能的最简单方法:

        switchWIdget.setOnClickListener{view -> 
                    val switchView = view as SwitchCompat
                    when (switchView.isChecked) {
                        false -> {
                            //do stuff for FALSE
                        }
                        true -> {
                           //do stuff for TRUE
                        }
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 2016-08-09
          • 2013-02-25
          • 2012-05-29
          • 2013-02-07
          • 2010-10-31
          • 1970-01-01
          • 2018-09-22
          • 2012-05-06
          • 2021-08-20
          相关资源
          最近更新 更多