例如:
我想要做的是有一个按钮,当按下它时,它会将所有 15 个切换按钮重置为关闭状态,以便用户可以重新开始。
我在最新的 Android Studio 中使用 Java 而不是 Kotlin
我对开发应用程序非常陌生,并尝试搜索我的问题,但由于我使用的非技术术语,我可能找不到答案。
我会将 15 个按钮的选中状态存储在列表中,每次用户修改开关的状态时,您都会在给定位置更新列表。
当用户选择重置按钮时,您只需将列表中的所有元素设置为 false。
Switch
对象,为重置按钮创建 Button
对象:// note that the names in R.id.name must match the IDs you assigned in your XML
Switch switch1 = (Switch) findViewById(R.id.switch1);
Switch switch2 = (Switch) findViewById(R.id.switch2);
Switch switch3 = (Switch) findViewById(R.id.switch3);
// ...
Button resetButton = (Button) findViewById(R.id.resetButton);
onClick
方法中添加默认值:resetButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
switch1.setChecked(true);
switch2.setChecked(false);
switch3.setChecked(true);
// ...
}
});
有关更多信息,请参阅 Android 开关文档,或参阅像这样的教程以获得更易理解/详细的分步说明。