我正在制作一个设置页面,其中包含一系列复选框。用户选择他想要的选项,然后点击提交按钮,将结果保存在文本文件中,如1; 0; 1; 1; 1; 0等,其中1代表已选中且0未选中。
下一个程序启动后,程序将查找设置文件。如果找不到,则复选框保留为默认的真实值(全部打开)。如果可以,它将读取文件,并相应地设置复选框。最后一步是我遇到问题 - 使用toasts,我可以看到程序找到文件,正确分割它,并且具有给定复选框的正确值。我甚至有一个在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" />
答案 0 :(得分:2)
问题可能是,
这意味着您应该更多地了解Android Activity Lifecycle。
此外,您的设置文件不是首选项的良好实现。除非您希望1个应用程序读取另一个Android应用程序的设置,否则您应该查看SharedPreferences以存储应用程序范围的设置 - 它真的很容易使用。
如果您想要一个简单的用户界面并希望快速完成,那么特别针对设置界面设计了一项特殊活动:PreferenceActivity - 尽管稍后您可能会发现它缺少某些功能。