【问题标题】:can't programmatically change toggle button无法以编程方式更改切换按钮
【发布时间】:2013-07-25 09:19:33
【问题描述】:

我似乎无法以编程方式将 ToggleButton 设置为关闭 (false) - 我已经做了很多搜索和尝试我发现的东西,但就是无法让它工作。相关代码就在这里-

  while (toggleButton4_.isChecked()){
seekBar_.setProgress(0);
toggleButton2_.setSelected(false);  
pwmOutput1_.setDutyCycle(0);        
pwmOutput2_.setPulseWidth(500);     
  }

seekbar设置为零ok,两个pwm输出分别为零和500,但是toggleButton和它的pin没有改变。

我会很高兴得到任何帮助,越简单越好,因为我是 java 的初学者。

谢谢,肯。

更新-我也尝试了 toggle(),结果相同,所以你是对的,其他地方一定有错误。这是我的应用程序的完整主要活动-

    package ioio.examples.hello;


import ioio.lib.api.DigitalInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.DigitalOutput.Spec.Mode;
import ioio.lib.api.PwmOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import kens.tws.R;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.ToggleButton;



public class MainActivity extends IOIOActivity {
    private Button button1_;
        private Button button2_;
    private SeekBar seekBar_;
    private ToggleButton toggleButton1_;
    private ToggleButton toggleButton2_;
    private ToggleButton toggleButton3_;
    private ToggleButton toggleButton4_;

        /**
     * Called when the activity is first created. Here we normally initialize
     * our GUI.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);


        seekBar_ = (SeekBar) findViewById(R.id.seekBar1);
        toggleButton1_ = (ToggleButton) findViewById(R.id.frButton);
        toggleButton2_ = (ToggleButton) findViewById(R.id.fieldButton);
        toggleButton3_ = (ToggleButton) findViewById(R.id.lightsButton);
        toggleButton4_ = (ToggleButton) findViewById(R.id.emBrakeButton);
        button1_ = (Button) findViewById(R.id.engineStartButton);
        button2_ = (Button) findViewById(R.id.hornButton);

    }

    /**
     * This is the thread on which all the IOIO activity happens. It will be run
     * every time the application is resumed and aborted when it is paused. The
     * method setup() will be called right after a connection with the IOIO has
     * been established (which might happen several times!). Then, loop() will
     * be called repetitively until the IOIO gets disconnected.
     */
    class Looper extends BaseIOIOLooper {

        private DigitalOutput engineStartButton_;
        private DigitalOutput hornButton_;
        private DigitalOutput fieldButton_;
        private DigitalOutput frButton_;
        private DigitalOutput lightsButton_;
        private DigitalOutput emBrakeButton_;
        private PwmOutput pwmOutput1_;
        private PwmOutput pwmOutput2_;
        private final int brakeOnPin = 18 ;
        private DigitalInput brakeOn ;

        /**
         * Called every time a connection with IOIO has been established.
         * Typically used to open pins.
         * 
         * @throws ConnectionLostException
         *             When IOIO connection is lost.
         * 
         * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
         */
        @Override
        protected void setup() throws ConnectionLostException {
            engineStartButton_ = ioio_.openDigitalOutput(3, false);
            hornButton_ = ioio_.openDigitalOutput(6, false);
            frButton_ = ioio_.openDigitalOutput(14, false);
            fieldButton_ = ioio_.openDigitalOutput(11, false);
            lightsButton_ = ioio_.openDigitalOutput(13, false);
            emBrakeButton_ = ioio_.openDigitalOutput(16, false);
            pwmOutput1_ = ioio_.openPwmOutput(5, 100);  //alternator field control
            pwmOutput2_ = ioio_.openPwmOutput(new DigitalOutput.Spec(10, Mode.OPEN_DRAIN), 100); //throttle servo
            brakeOn = ioio_.openDigitalInput(brakeOnPin, DigitalInput.Spec.Mode.PULL_UP);
            }


        /**
         * Called repetitively while the IOIO is connected.
         * @throws ConnectionLostException
         *             When IOIO connection is lost.
         * 
         * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
         */

    @Override
    public void loop() throws ConnectionLostException {
        pwmOutput1_.setPulseWidth(seekBar_.getProgress() * 100);  //alternator field
        pwmOutput2_.setPulseWidth(500 + seekBar_.getProgress() * 14);  //engine throttle
        engineStartButton_.write(button1_.isPressed());
        hornButton_.write(button2_.isPressed());
        frButton_.write(!toggleButton1_.isChecked());
        fieldButton_.write(toggleButton2_.isChecked());
        lightsButton_.write(toggleButton3_.isChecked());
        //  emBrakeButton_.write(toggleButton4_.isChecked());

        while (toggleButton4_.isChecked()){
        // Emergency brake is on, so turn field relay off, zero seekbar, set field switch
        //  to off, and set both pwm's to zero. 
            seekBar_.setProgress(0);
            fieldButton_.write(false);
            toggleButton2_.setChecked(false);  //alternator field switch
            pwmOutput1_.setDutyCycle(0);        //alternator field winding pwm
            pwmOutput2_.setPulseWidth(500);     //engine throttle servo 
        }  

        try {
                if (!brakeOn.read()) {
                   // brake pin is pulled low, so zero pwm and seekBar
                    seekBar_.setProgress(0);
                    pwmOutput1_.setDutyCycle(0);
                    pwmOutput2_.setPulseWidth(500);
                }

            } catch (InterruptedException e) {
                // do nothing
            }
          try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }


    } // End class Looper
    /**
     * A method to create our IOIO thread.
     * 
     * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
     */
        @Override
        protected IOIOLooper createIOIOLooper() {

            return new Looper();
        }


    } // End class MainActivity

另外,这里是相关按钮的xml-

  <ToggleButton
    android:id="@+id/fieldButton"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_column="0"
    android:layout_gravity="center_horizontal|top"
    android:layout_row="0"
    android:background="@drawable/field_button_toggle"
    android:checked="false"
    android:text="@string/field"
    android:textOff="@string/field_off"
    android:textOn="@string/field_on" />

我也使用colors.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
    <color name="blue">#0000ff</color>
    <color name="orange">#ff8800</color>
    <color name="white">#ffffff</color>
</resources>

还有另一个用于可绘制字段按钮的 xml-

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@color/green"  />
    <item android:state_checked="true" android:drawable="@color/red"  />
</selector>

谢谢,我真的很感激你为我所做的一切, 肯。

【问题讨论】:

  • 上面的代码中有两个不同的切换按钮,是故意的吗?
  • while (toggleButton4_.isChecked()) 这个循环什么时候循环 break
  • 是的,在我看来就像一个无限循环。 :)
  • 并使用toggleButton2_.setChecked(false); ;)

标签: android togglebutton


【解决方案1】:

您正在寻找:

toggleButton2_.setChecked(false);

【讨论】:

    【解决方案2】:

    改用这个:

    if (toggleButton4_.isChecked()){
      seekBar_.setProgress(0);
      toggleButton2_.setChecked(false);  
      pwmOutput1_.setDutyCycle(0);        
      pwmOutput2_.setPulseWidth(500);     
      }
    

    干杯:)

    【讨论】:

      【解决方案3】:

      你要做的是使用:(1)

      toggleButton2_.setChecked(false);
      

      所以它看起来像这样:

      while (toggleButton4_.isChecked()){
          seekBar_.setProgress(0);
          toggleButton2_.setChecked(false);  
          pwmOutput1_.setDutyCycle(0);         
          pwmOutput2_.setPulseWidth(500);     
      }
      

      但我不明白你的循环。它是无限的。而不是使用while 使用if。所以最后会变成:(2)

      if (toggleButton4_.isChecked()){
          seekBar_.setProgress(0);
          toggleButton2_.setChecked(false);  
          pwmOutput1_.setDutyCycle(0);         
          pwmOutput2_.setPulseWidth(500);     
      }
      

      但是是的,这取决于您在做什么。这两件事应该可以解决问题。

      【讨论】:

      • 感谢大家的建议。澄清一下,该应用程序是控制我的一个微型机车(见vk7krj.com/Petrolelectric1.htm
      • 感谢大家的建议。澄清一下,该应用程序是控制我的一个微型机车(见本页底部的红色机车vk7krj.com/Petrolelectric1.htm)。 ToggleButton4 是紧急停止按钮,pwm2 是发动机油门,pwm1 控制交流发电机,togglebutton2 操作交流发电机电路中的继电器。这个循环的目的是当紧急停止按钮被按下时,一切都会关闭,直到紧急停止按钮被切换。我尝试将其更改为 .setChecked,它没有更改 togglebutton2,但我确实失去了对应用程序的控制。
      • 如果需要,我可以发布更多代码,但作为一个等级初学者,我不确定需要哪些位来帮助诊断我做错了什么。
      • 为了澄清一点,我尝试了这两个建议 - 更改为 .setChecked(false);并从 while 更改为 if。两者都没有产生预期的效果。随着 .setChecked 的更改,按钮在视觉上像以前一样工作,但我的硬件没有产生任何输出。要么更改回 set.Selected,要么完全注释掉该行,使硬件再次工作。我花了一个半星期的时间试图解决这个问题——它让我撕掉了我剩下的一点头发!如果有助于找出我做错了什么,我很乐意将整个项目放在我的网站上。
      • 你可以试试.toggle()。我尝试使用两个切换按钮以及 isChecked() 和 setChecked()。他们在工作。请更新您的问题以包含使用您发布的代码的代码。可能还有其他错误。
      猜你喜欢
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 2010-12-21
      • 1970-01-01
      相关资源
      最近更新 更多