简体   繁体   English

如何使用Android Studio中的开关小部件使function()启用/禁用?

[英]how to make a function() enable/disable using switch widget in android studio?

I got a function called updateTv() in my program which will run as soon as the app is opened. 我的程序中有一个名为updateTv()的函数,该函数将在打开应用程序后立即运行。 Now what I want is to create a button which will enable and disable the function updateTv(), so when the app is opened, the function updateTv() should run only when the switch is on/enable and when the switch is off, it should be disabled. 现在,我要创建一个按钮,该按钮将启用和禁用功能updateTv(),因此,当打开应用程序时,功能updateTv()应该仅在开关打开/启用且开关关闭时才运行应该被禁用。

Here is my code: 这是我的代码:

final Runnable updater = new Runnable() {

    public void run() {
        updateTv();
        push(mEMA);

    }

};

after few suggestions, I tried this, 经过几个建议,我尝试了这个,

final Runnable updater = new Runnable() {
        public void run() {
            aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        {updateTv();
                            push(mEMA);
                        }
                        aSwitch.setText("disable");
                    } else {
                        aSwitch.setText("enable");
                    }
                }
            });
        }
    };
    final Handler mHandler = new Handler();

still there's a problem, whenever it is switched on, its runs only once 仍然有一个问题,无论何时打开,它只能运行一次

You can achieve this by using SharedPreference in your project. 您可以通过在项目中使用SharedPreference来实现。 Suppose you have Enabled and Disabled button in the layout, when you click on Enabled you will store boolean in your shared preference with value as true, 假设您在布局中具有“已启用”和“已禁用”按钮,则当您单击“已启用”时,会将布尔值存储在共享首选项中,其值为true,

Now when you start your application you will get the value from sharedpreference about Enabled/Disabled and move ahead with your code. 现在,当您启动应用程序时,您将从sharedpreference中获取有关Enabled / Disabled的值,然后继续执行代码。

final Runnable updater = new Runnable() {

            public void run() {
              if(isEnabled){
                updateTv();
                push(mEMA);
              }
            }


        };

try this: 尝试这个:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class test {

    public static volatile boolean activated = false;

    public test() {

         JFrame q = new JFrame();
         q.setLayout(null);
         q.setSize(400, 400);
         q.setLocationRelativeTo(null);
         q.setTitle("Tv something?");

         JRadioButton button = new JRadioButton("on/off");
         button.setSize(100, 40);
         button.setLocation(q.getWidth()/2 - button.getWidth()/2, q.getHeight()/2 - button.getHeight()/2);
         button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (button.isSelected()) {
                    activated = true;
                } else {
                    activated = false; 
                }
            }
         });
         q.add(button);

         q.setVisible(true);

    }


}

now take that (volatile) boolean and put it inside your runnable like Silvans Solanki suggested like this: 现在,将其(易失性)布尔值像Silvans Solanki所建议的那样放在您的可运行对象中:

final Runnable updater = new Runnable() {

        public void run() {
          if(activated){
            updateTv();
            push(mEMA);
          }
        }


    };

edit: 编辑:

for android u can do something like this: 对于android,您可以执行以下操作:

Button btn= new Button(this);  
btn.settext("Submit");  
btn.setOnClickListener(new View.OnClickListener()   
{
    public void onClick(View view) 
     {
           activated = !activated;
       }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM