0

我正在尝试在我的 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。

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

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

4

4 回答 4

1

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

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

最后,在 onCheckChanged 开始时,你做

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

于 2018-02-04T16:00:45.347 回答
0

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

于 2018-02-04T15:43:20.097 回答
0

您可以通过删除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);
于 2018-02-04T15:54:13.767 回答
0

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

switchWIdget.setOnClickListener{view -> 
            val switchView = view as SwitchCompat
            when (switchView.isChecked) {
                false -> {
                    //do stuff for FALSE
                }
                true -> {
                   //do stuff for TRUE
                }
            }
        }
于 2020-05-20T11:44:12.377 回答