我正在制作一个控制8个继电器的应用程序。点是我是编程新手,代码显示所有类型的错误,同时使用2个以上的切换按钮。我希望下面将打开/关闭切换时将所需的命令发送到我的arduino。但是当我有很多切换时(例如在我的情况下4切换)我如何使用if / else,当我关闭应用程序或在其他设备中使用应用程序时我还需要持续状态。猜猜它有很多要问。
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
答案 0 :(得分:0)
请找到以下解决方案..它适用于您
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;
}
}
}