我正试图在我的android应用程序中实现一个设置活动,用户可以在其中打开或关闭通知模式。为了达到这个目的,我使用了开关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));
}
}
});我想做什么?
怎么啦?
为什么会发生这种情况?
我试过什么?
有人知道我怎样才能克服这一切吗。提前谢谢。
发布于 2018-02-04 16:00:45
您可以使用标志作为类属性,在对话框中单击选项时设置true,如IsFromDialog。
然后,在对话框中的Onclick函数中,设置IsFromDialog = true;
最后,在onCheckChanged开始时
如果(!b & !IsFromDialog) {.}
发布于 2018-02-04 15:43:20
当用户单击该开关时,OnCheckChanged侦听器将被触发一次,然后当您的对话框将其更改回时再次触发它,这就是循环。尝试使用OnTouchListener代替。
发布于 2018-02-04 15:54:13
您可以通过删除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);https://stackoverflow.com/questions/48609286
复制相似问题