0

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

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

seekbar 设置为零,两个 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>

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

4

3 回答 3

0

你需要做的是使用:(1)

toggleButton2_.setChecked(false);

所以它看起来像这样:

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

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

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

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

于 2013-07-23T14:59:21.950 回答
0

您正在寻找:

toggleButton2_.setChecked(false);
于 2013-07-23T14:03:09.900 回答
0

改用这个:

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

干杯:)

于 2013-07-23T14:04:05.753 回答