android - ToggleButton 状态 OnClick

标签 android variables onclick onclicklistener togglebutton

如何更改 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/

相关文章:

android - 通过 ADB 安装新的启动器应用程序时,未清除家庭默认应用程序

android - 应用程序图标未显示在 Xamarin.Forms Android 应用程序中

android - 如何改变ImageView中触摸区域的颜色?

php - 如何使用 php 降低 sql 值

javascript - React中一键连续调用函数

android - 方向更改时应用程序崩溃(就在我为纵向和横向模式设置 2 个不同的背景时)

javascript - Javascript 变量提升的使用不正确?

php - 如果未设置变量,则使用函数初始化变量 - PHP

android - 双音安卓

javascript - 使用 Javascript onclick 更改图像 alt