我似乎无法以编程方式将ToggleButton设置为关闭(false) - 我已经做了很多搜索并尝试了我找到的东西,但却无法使其工作。相关代码在这里 -
while (toggleButton4_.isChecked()){
seekBar_.setProgress(0);
toggleButton2_.setSelected(false);
pwmOutput1_.setDutyCycle(0);
pwmOutput2_.setPulseWidth(500);
}
搜索条设置为零,两个pwm输出分别为零和500,但是toggleButton和它的引脚不会改变。
我会很高兴有任何帮助,越简单就越好,因为我非常喜欢java。
谢谢,Ken。
更新 - 我也尝试了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 drawable -
<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>
谢谢,我真的很感谢你为我做的事, 肯
答案 0 :(得分:0)
您正在寻找:
toggleButton2_.setChecked(false);
答案 1 :(得分:0)
请改用:
if (toggleButton4_.isChecked()){
seekBar_.setProgress(0);
toggleButton2_.setChecked(false);
pwmOutput1_.setDutyCycle(0);
pwmOutput2_.setPulseWidth(500);
}
干杯:)
答案 2 :(得分:0)
您需要做的是:(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);
}
但是这取决于你在做什么。这两件事应该可以解决问题。