简体   繁体   English

togglebutton.isChecked()无法正常工作?

[英]togglebutton.isChecked() is not working?

I am working with an Arduino and trying use two toggle buttons to connect and enable the Arduino. 我正在使用Arduino,并尝试使用两个切换按钮来连接并启用Arduino。 I don't want to be able to press the enable button until I have connected the robot. 在连接机器人之前,我不希望能够按启用按钮。 Here is my main activity: 这是我的主要活动:

public class MainActivity extends Activity {

Arduino arduino = null;
ToggleButton enable, connect;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ToggleButton connect = (ToggleButton) findViewById(R.id.buttonConnect);
    ToggleButton enable = (ToggleButton) findViewById(R.id.buttonEnable);

}

public void onToggleClicked(View view) {


    switch(view.getId()) {

        case R.id.buttonConnect:

            boolean on = ((ToggleButton) view).isChecked();

            if(on){

                //Take the text from editText1 and make it the IP Address
                EditText text = (EditText)findViewById(R.id.editText1);
                String ip = text.getText().toString();
                arduino = new Arduino(ip);

                arduino.connect();

                arduino.disable();

            } else {

                arduino.disable();
                arduino.disconnect();
                enable.setChecked(false);
            }
        break;  



        case R.id.buttonEnable:

            boolean click = ((ToggleButton) view).isChecked();

            if(click && connect.isChecked()) {

                arduino.enable();

            }else {

                System.out.println("Not Connected");
                enable.setChecked(False);
                arduino.disable();
            }
        break;  

        default:

        break;  

        }

    }

I know the program is not reaching the else statement in the buttonEnable case because it is not printing out "Not Connected." 我知道该程序未达到buttonEnable情况下的else语句,因为它没有打印出“未连接”。 I have also tried to use connect.isEnabled() in my if statement but that doesn't work either. 我也尝试在我的if语句中使用connect.isEnabled() ,但这也不起作用。 Does anybody have answer? 有人回答吗? Thank you! 谢谢!

尝试设置点击侦听器以将代码执行附加到您的按钮

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

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