Android复选框setChecked()无效
我正在做一个设置页面,里面有一堆复选框。用户选择他想要的选项,然后点击提交按钮,将结果保存在文本文件中,如1;0;1.1.1.0等,1表示选中,0表示未选中 在下一个程序启动时,程序将查找设置文件。如果找不到,复选框将保留默认的真值(全部打开)。如果可以,它将读取文件,并相应地设置复选框。最后一步是我遇到问题的地方-使用toasts,我可以看到程序找到文件,正确地分割它,并且具有给定复选框的正确值。我甚至在if()块中触发了一个toast,在该块中我检查值是否为0,即使用setChecked()代码后的行。toast启动,因此正在读取setChecked()代码。但是,复选框不会更新,而是保持选中状态。我的猜测是,在我更改框之后,视图不会被刷新 这是我第一次做一个android选项,它不完全是一个服务,并且有一个GUI,所以我对它有点不清楚。设置框后快速刷新屏幕的最简单方法是什么,或者是否存在其他问题Android复选框setChecked()无效,android,android-checkbox,Android,Android Checkbox,我正在做一个设置页面,里面有一堆复选框。用户选择他想要的选项,然后点击提交按钮,将结果保存在文本文件中,如1;0;1.1.1.0等,1表示选中,0表示未选中 在下一个程序启动时,程序将查找设置文件。如果找不到,复选框将保留默认的真值(全部打开)。如果可以,它将读取文件,并相应地设置复选框。最后一步是我遇到问题的地方-使用toasts,我可以看到程序找到文件,正确地分割它,并且具有给定复选框的正确值。我甚至在if()块中触发了一个toast,在该块中我检查值是否为0,即使用setChecked()
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//establish checkBoxes
checkBatteryLevel = (CheckBox) findViewById(R.id.checkBox1);
checkBatteryVoltage = (CheckBox) findViewById(R.id.checkBox2);
checkBluetooth = (CheckBox) findViewById(R.id.checkBox5);
checkCall = (CheckBox) findViewById(R.id.checkBox4);
checkCharger = (CheckBox) findViewById(R.id.checkBox3);
checkScreen = (CheckBox) findViewById(R.id.checkBox8);
checkWifiPermis = (CheckBox) findViewById(R.id.checkBox6);
checkWifiCnct = (CheckBox) findViewById(R.id.checkBox7);
submitButton = (Button) findViewById(R.id.button1);
Toast toast = Toast.makeText(getApplicationContext(), "Text here", Toast.LENGTH_LONG);
//check if text settings are there
textSettings = new File (root, "UsageMonitorSettings.txt");
if(textSettings.exists())
{
toast = Toast.makeText(getApplicationContext(), "File Exists", Toast.LENGTH_LONG);
toast.show();
//if present, read it and set buttons accordingly
try {
Scanner myScanner = new Scanner(textSettings);
while (myScanner.hasNextLine())
{
String line = myScanner.next();
toast = Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG);
toast.show();
String[] lineArray = line.split(";");
toast = Toast.makeText(getApplicationContext(), lineArray[0], Toast.LENGTH_LONG);
toast.show();
if(lineArray[0].equals("0"))
{
checkBatteryLevel.setChecked(false);
toast = Toast.makeText(getApplicationContext(), "batt check changed", Toast.LENGTH_LONG);
toast.show();
}
if(lineArray[1].equals("0"))
{
checkBatteryVoltage.setChecked(false);
}
if(lineArray[2].equals("0"))
{
checkCharger.setChecked(false);
}
if(lineArray[3].equals("0"))
{
checkCall.setChecked(false);
}
if(lineArray[4].equals("0"))
{
checkBluetooth.setChecked(false);
}
if(lineArray[5].equals("0"))
{
checkWifiPermis.setChecked(false);
}
if(lineArray[6].equals("0"))
{
checkWifiCnct.setChecked(false);
}
if(lineArray[7].equals("0"))
{
checkScreen.setChecked(false);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
toast = Toast.makeText(getApplicationContext(), "File does not exist", Toast.LENGTH_LONG);
toast.show();
//if not, make one with all defaulting to on
try {
BufferedWriter myFileWriter = new BufferedWriter(new FileWriter(textSettings));
myFileWriter.write("1;1;1;1;1;1;1;1");
myFileWriter.flush();
myFileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//when button hit update text settings with checked values
try {
BufferedWriter myFileWriter = new BufferedWriter(new FileWriter(textSettings, true));
if(checkBatteryLevel.isChecked())
{
myFileWriter.write("1"+";");
}
else
{
myFileWriter.write("0"+";");
}
if(checkBatteryVoltage.isChecked())
{
myFileWriter.write("1"+";");
}
else
{
myFileWriter.write("0"+";");
}
if(checkCharger.isChecked())
{
myFileWriter.write("1"+";");
}
else
{
myFileWriter.write("0"+";");
}
if(checkCall.isChecked())
{
myFileWriter.write("1"+";");
}
else
{
myFileWriter.write("0"+";");
}
if(checkBluetooth.isChecked())
{
myFileWriter.write("1"+";");
}
else
{
myFileWriter.write("0"+";");
}
if(checkWifiPermis.isChecked())
{
myFileWriter.write("1"+";");
}
else
{
myFileWriter.write("0"+";");
}
if(checkWifiCnct.isChecked())
{
myFileWriter.write("1"+";");
}
else
{
myFileWriter.write("0"+";");
}
if(checkScreen.isChecked())
{
myFileWriter.write("1");
}
else
{
myFileWriter.write("0");
}
myFileWriter.flush();
myFileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
下面是main.xml的相关部分:
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:text="Battery Level"
android:checked="true" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Battery Voltage"
android:checked="true" />
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth State"
android:checked="true" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call State"
android:checked="true" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Charger State"
android:checked="true" />
<CheckBox
android:id="@+id/checkBox8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen State"
android:checked="true" />
<CheckBox
android:id="@+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wifi Permission"
android:checked="true" />
<CheckBox
android:id="@+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wifi Connection"
android:checked="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Service" />
问题可能是
- 使用onCreate,首先读取设置(如果存在),然后创建onClickListeners
- 你在简历上的方法是什么?你在那里做什么李>
这意味着您应该更深入地了解
此外,您的设置文件不是首选项的良好实现。除非您想让一个应用程序读取另一个Android应用程序的设置,否则您应该研究如何存储应用程序范围的设置—它非常易于使用
如果您想要一个简单的用户界面,并且想要快速完成,那么有一个专门为设置用户界面设计的特殊活动:-尽管稍后您可能会发现它缺少一些功能。问题可能是
- 使用onCreate,首先读取设置(如果存在),然后创建onClickListeners
- 你在简历上的方法是什么?你在那里做什么李>
这意味着您应该更深入地了解
此外,您的设置文件不是首选项的良好实现。除非您想让一个应用程序读取另一个Android应用程序的设置,否则您应该研究如何存储应用程序范围的设置—它非常易于使用
如果您想要一个简单的UI,并且想要快速完成,那么有一个专门为设置UI设计的特殊活动:-尽管稍后您可能会发现它缺少一些功能