我正在尝试在我的 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。
我也尝试使用标志变量,但我找不到逻辑。
有人知道我如何克服这个问题。提前致谢。