android旋转边框,确定对话框后,android如何翻转 switch的选中状态?_android_开发99编程知识库...

使用下列更新的代码。sw_enableDisable

. setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

if (toogledProgrammatically) {

toogledProgrammatically = false;

} else {

if (isChecked) {

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(

MainActivity.this);

alertDialogBuilder

. setMessage(

"Sure you want to enable?.")

. setCancelable(true)

. setPositiveButton(

"YES",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

dialog.cancel();

}

})

. setNegativeButton(

"NO",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

dialog.cancel();

toogledProgrammatically = true;

sw_enableDisable.toggle();

}

});

AlertDialog alertDialog = alertDialogBuilder.create();

alertDialog.show();

} else {

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(

MainActivity.this);

alertDialogBuilder

. setMessage(

"Sure you want to disable?")

. setCancelable(true)

. setPositiveButton(

"YES",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

dialog.cancel();

}

})

. setNegativeButton(

"NO",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

dialog.cancel();

toogledProgrammatically = true;

sw_enableDisable.toggle();

}

});

AlertDialog alertDialog = alertDialogBuilder.create();

alertDialog.show();

}

}

}

});

因此,每当用户单击 No的时,就应该使用sw_enableDisable.toggle();

但这又会叫 onCheckedChanged() 然后再一次循环。 所以,要处理这个问题,在你切换到代码时,维护一个布尔值 toogledProgrammatically 并将它的设置为 true 。 然后调用 onCheckedChanged() 时,只需检查它是否以编程方式被切换,或者。 如果是的话,不要做任何事,否则显示警报。if (toogledProgrammatically) {

toogledProgrammatically = false;

} else {

if (isChecked)

.

.

.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值