开关兼容性:onCheckedChanged
Switch Compat: onCheckedChanged
我正在尝试在我的 android 应用程序中实施设置 activity,用户可以在其中打开和关闭通知模式。为此,我正在使用 Switch Compat。这是布局。这是我的 activity 布局的一部分。
<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));
}
}
});
我想做什么?
- 正如我所说,这应该让用户能够打开和关闭通知。
怎么了?
- 代码工作正常,除了一个错误,该错误是当用户想要为 ex 打开通知时。然后改变主意并在他将循环进入的对话框中单击“取消”。
为什么会这样?
- 我已取消以这种方式工作,因此如果用户单击“取消”,Switch Compat 将返回到之前的位置。但是因为我有那个状态的方法所以它会触发另一个状态并且用户处于循环中。
我尝试了什么?
我尝试使用 switch,但无法传递布尔变量 b。
我也试过使用标志变量,但找不到逻辑。
有人知道我该如何克服这个问题吗?
提前致谢。
OnCheckChanged 侦听器在用户单击开关时触发一次,然后当您的对话框将其改回时再次触发,因此循环。尝试改用 OnTouchListener。
您可以通过删除 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);
从这里开始,剩下的就很简单了。进入所有四个 positive/negative 听众,并使它们看起来像这样:
notificationsSwitch.setOnCheckedChangeListener(null);
notificationsSwitch.setChecked(true);
notificationsSwitch.setOnCheckedChangeListener(listener);
您可以使用标志作为 class 属性,当单击对话框中的选项时将设置为真,例如 IsFromDialog。
然后,在对话框的 Onclick 函数中,设置
IsFromDialog = true;
最后,在您执行的 onCheckChanged 开始时
如果 (!b && !IsFromDialog) {
...
}
实现相同功能的最简单方法:
switchWIdget.setOnClickListener{view ->
val switchView = view as SwitchCompat
when (switchView.isChecked) {
false -> {
//do stuff for FALSE
}
true -> {
//do stuff for TRUE
}
}
}
我正在尝试在我的 android 应用程序中实施设置 activity,用户可以在其中打开和关闭通知模式。为此,我正在使用 Switch Compat。这是布局。这是我的 activity 布局的一部分。
<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));
}
}
});
我想做什么?
- 正如我所说,这应该让用户能够打开和关闭通知。
怎么了?
- 代码工作正常,除了一个错误,该错误是当用户想要为 ex 打开通知时。然后改变主意并在他将循环进入的对话框中单击“取消”。
为什么会这样?
- 我已取消以这种方式工作,因此如果用户单击“取消”,Switch Compat 将返回到之前的位置。但是因为我有那个状态的方法所以它会触发另一个状态并且用户处于循环中。
我尝试了什么?
我尝试使用 switch,但无法传递布尔变量 b。
我也试过使用标志变量,但找不到逻辑。
有人知道我该如何克服这个问题吗? 提前致谢。
OnCheckChanged 侦听器在用户单击开关时触发一次,然后当您的对话框将其改回时再次触发,因此循环。尝试改用 OnTouchListener。
您可以通过删除 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);
从这里开始,剩下的就很简单了。进入所有四个 positive/negative 听众,并使它们看起来像这样:
notificationsSwitch.setOnCheckedChangeListener(null);
notificationsSwitch.setChecked(true);
notificationsSwitch.setOnCheckedChangeListener(listener);
您可以使用标志作为 class 属性,当单击对话框中的选项时将设置为真,例如 IsFromDialog。
然后,在对话框的 Onclick 函数中,设置 IsFromDialog = true;
最后,在您执行的 onCheckChanged 开始时
如果 (!b && !IsFromDialog) { ... }
实现相同功能的最简单方法:
switchWIdget.setOnClickListener{view ->
val switchView = view as SwitchCompat
when (switchView.isChecked) {
false -> {
//do stuff for FALSE
}
true -> {
//do stuff for TRUE
}
}
}