我正在設置一個設置頁面,並帶有一堆複選框。用戶選擇他想要的選項,然後點擊提交按鈕,將結果保存在文本文件上,如1; 0; 1; 1; 1; 0等,其中1代表選中,0選中。Android複選框setChecked()不起作用
下一次程序啓動時,程序將查找設置文件。如果找不到,複選框會保留默認值(全部開啓)。如果可以,它會讀取文件,並相應地設置複選框。最後一步是我遇到問題的地方 - 使用吐司,我可以看到程序找到了文件,將其正確拆分,並且對於給定的複選框具有正確的值。我甚至在if()塊中檢查是否有一個烤麪包,我檢查該值是否爲0,這是我使用setChecked()代碼後的行。這吐司火,所以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" />