如何更改 toggleButton 的状态 只有在我单击对话框上的 OK 之后,如果我单击 Cancel 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();
}
最佳答案
据我了解,您的问题似乎很容易解决,请考虑此代码
1-
alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
toggelBt.toggle();
}
});
2- 在其他方式上,您可以在每次调用 ToggleButton Onclick 方法时将运行时状态保存在 bool 变量中(使用 sharedPreference 而不是 bool 值来保存并在下次应用程序启动时使用)然后
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);
}
});
努力成为有用的人:)
关于android - ToggleButton 状态 OnClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20932991/