我的问题是 - 当我点击 a 时<Switch>
,它首先被切换,然后OnCheckedChangeListener
被调用。
我想要的是这样的:
<Switch>
被点击 --> 我显示一个 AlertDialog --> 如果按下是或否 --> 然后用setChecked(boolean)翻转` ,[boolean = true 如果按下yes, false 如果按下 no]。
问题:<Switch>
单击时,它会自动翻转。我想先用 AlertDialog 中的是或否来限定它。
sw_enableDisable
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());
alertDialogBuilder
.setMessage(
"Sure you want to enable?. ")
.setCancelable(true)
.setPositiveButton(
getString("YES"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
})
.setNegativeButton(
getString("NO"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
sw_enDis_alreadyClicked = true;
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());
alertDialogBuilder
.setMessage(
"Sure you want to disable?")
.setCancelable(true)
.setPositiveButton(
getString("YES"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
})
.setNegativeButton(
getString("NO"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});