【发布时间】: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