如何在对话框中单击“确定”后更改toggleButton的状态,如果单击“取消”toggleButton未更改,我的问题是toggleButton始终切换,那么是否有需要的示例或特定实现以前要做什么?
toggle1 = (ToggleButton) findViewById(R.id.filterButton);
// toggle1.setChecked(getDefaults("Toggle1S",this));
// setDefaults("Toggle1S", toggle1.isChecked(), this);
toggle1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (toggle1.isChecked()) {
on.start();
Using();
if(Use.equals("1"))
{//Wifi Function
Toast.makeText(getApplicationContext(), "Filter ON sent using Wifi", Toast.LENGTH_SHORT).show();
new MyAsyncTask().execute("filter_st","ON");
}
else{
String temp = "Are you want to turn Filter ON using GSM";
callCheck(SMStitle,temp);
sm.sendTextMessage(number, null, messages[0], null, null);
Toast.makeText(getApplicationContext(), "Filter ON sent using GSM number"+number, Toast.LENGTH_SHORT).show();
}
public void callCheck(String c,String d)
{
// Creating alert Dialog with one Button
AlertDialog alertDialog1 = new AlertDialog.Builder(
fishtank.this).create();
// Setting Dialog Title
alertDialog1.setTitle(c);
// Setting Dialog Message
alertDialog1.setMessage(d);
// Setting OK Button
alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
alertDialog1.setButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog1.show();
}
答案 0 :(得分:2)
据我所知,您的问题似乎很容易解决,请考虑此代码为1 -
alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
toggelBt.toggle();
}
});
2-以及其他方式你可以在每个调用ToggleButton Onclick方法的布尔变量中保存你的State On运行时(使用 sharedPreference 而不是boolean来保存并在下一个app启动时使用)然后
Boolean CheckState
...
alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(checkState)//should set uncheck now
toggelBt.setChecked(false);
else
toggelBt.setChecked(true);
}
});
跳跃有用:)