我正在制作一个带有一堆复选框的设置页面。用户选择他想要的选项,然后点击提交按钮,将结果保存在文本文件中,如 1;0;1;1;1;0 等,其中 1 表示选中,0 表示未选中。
在下一个程序启动时,程序将查找设置文件。如果找不到它,复选框将保留在其默认的真实值(全部打开)。如果可以,它将读取文件,并相应地设置复选框。最后一步是我遇到问题的地方 - 使用 toast,我可以看到程序找到了文件,正确地拆分它,并且对于给定的复选框具有正确的值。我什至在 if() 块中触发了一个 toast,在该块中我检查值是否为 0,即使用 setChecked() 代码之后的行。该 toast 触发,因此正在读取 setChecked() 代码。但是,该复选框不会更新,并保持选中状态。我的猜测是在我更改框后视图没有被刷新。
这是我第一次制作一个不完全是服务的android选项,并且有一个GUI,所以我有点不清楚。设置框后快速刷新屏幕的最简单方法是什么,或者还有其他问题吗?
@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" />