首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >开关组件: onCheckedChanged

开关组件: onCheckedChanged
EN

Stack Overflow用户
提问于 2018-02-04 14:44:10
回答 4查看 3.2K关注 0票数 0

我正试图在我的android应用程序中实现一个设置活动,用户可以在其中打开或关闭通知模式。为了达到这个目的,我使用了开关Compat。这是布局。这是我活动计划的一部分。

代码语言:javascript
复制
 <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>

这是对开关组件的响应功能。

代码语言:javascript
复制
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));
                }
            }
        });

我想做什么?

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

怎么啦?

  • 除了一个bug之外,该代码运行良好,当用户希望为ex打开通知时,就会出现这个错误。然后改变主意,在他将进入循环的对话框上单击Cancel。

为什么会发生这种情况?

  • 我已经让取消工作的方式,因此,如果用户单击取消开关,Compat回到以前的位置,它是。但是,因为我有该状态的方法,所以它触发了另一个状态,并且用户处于循环状态。

我试过什么?

  • 我试过使用开关,但不能传递布尔变量b。
  • 我也尝试使用标志变量,但找不到逻辑。

有人知道我怎样才能克服这一切吗。提前谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-02-04 16:00:45

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

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

最后,在onCheckChanged开始时

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

票数 1
EN

Stack Overflow用户

发布于 2018-02-04 15:43:20

当用户单击该开关时,OnCheckChanged侦听器将被触发一次,然后当您的对话框将其更改回时再次触发它,这就是循环。尝试使用OnTouchListener代替。

票数 0
EN

Stack Overflow用户

发布于 2018-02-04 15:54:13

您可以通过删除OnCheckedChangeListener,然后修改SwitchCompat的状态,然后重新添加OnCheckedChangeListener来解决这个问题。这样,当你修改开关的时候,什么都听不到。

如果您重构一下代码,这将更容易实现。首先,应该将匿名侦听器提取到片段中的一个变量。换言之,删除这一段:

代码语言:javascript
复制
notificationsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    ...
});

并添加以下内容(在onCreateView()之外):

代码语言:javascript
复制
private CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
    ...
}

现在您已经这样做了,您可以在onCreateView()中设置它,如下所示:

代码语言:javascript
复制
notificationsSwitch = (SwitchCompat) view.findViewById(R.id.notificationsSwitch);
notificationsSwitch.setOnCheckedChangeListener(listener);

从这里开始,剩下的就很简单了。进入所有四个积极/消极的听众,并使他们看起来如下:

代码语言:javascript
复制
notificationsSwitch.setOnCheckedChangeListener(null);
notificationsSwitch.setChecked(true);
notificationsSwitch.setOnCheckedChangeListener(listener);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48609286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档