Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

Multiple toggle buttons android issue

i am making an app for controlling 8 relays. Point is i am new to programming and code shows all type of errors while using more than 2 toggle buttons.I hope below will send desired command to my arduino when toggle is on/off. but how do i use if/else when there are many toggles(say in my case 4 toggle) and i also need to last states when i close the app or use app in other device. guess its a lot to ask.

toggleButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (toggleButton.isChecked()) {
            commandArduino("  ");
    Toast.makeText(getApplicationContext(), "led2",Toast.LENGTH_LONG).show();
        } else {
            commandArduino("  ");
    Toast.makeText(getApplicationContext(), "led2",Toast.LENGTH_LONG).show();
        }
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please find the below solution..It will workable for you

public class Example extends Activity implements OnClickListener {

private ToggleButton button2;
private ToggleButton button1;
private ToggleButton button3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example);
    button1 = (ToggleButton) findViewById(R.id.toggleButton1);
    button2 = (ToggleButton) findViewById(R.id.toggleButton2);
    button3 = (ToggleButton) findViewById(R.id.togglebutton3);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);


}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.toggleButton1:
        if(button1.isChecked())
        {
            button1.setChecked(true);

            Toast.makeText(Example.this, "BUTTON1 UNCHECKED", Toast.LENGTH_LONG).show();

        }
        else
        {
            button1.setChecked(false);
            Toast.makeText(Example.this, "BUTTON1 CHECKED", Toast.LENGTH_LONG).show();
        }



        break;
    case R.id.toggleButton2:
        if(button2.isChecked())
        {
            button2.setChecked(true);
            Toast.makeText(Example.this, "BUTTON2 UNCHECKED", Toast.LENGTH_LONG).show();

        }
        else
        {
            button2.setChecked(false);
            Toast.makeText(Example.this, "BUTTON2 CHECKED", Toast.LENGTH_LONG).show();
        }

        break;
    case R.id.togglebutton3:
        if(button3.isChecked())
        {
            button3.setChecked(true);
            Toast.makeText(Example.this, "BUTTON3 UNCHECKED", Toast.LENGTH_LONG).show();

        }
        else
        {
            button3.setChecked(false);
            Toast.makeText(Example.this, "BUTTON3 CHECKED", Toast.LENGTH_LONG).show();
        }

        break;

    default:
        break;
    }
   }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...