2012-08-24 58 views
0

我正在设置一个设置页面,并带有一堆复选框。用户选择他想要的选项,然后点击提交按钮,将结果保存在文本文件上,如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" /> 

回答

2

的问题可能是,

  • 用的onCreate,你先阅读设置,如果存在的话,那么你做了onClickListeners。
  • 你在方法onResume中做什么?你在那里做什么?!

这意味着你应该多看看Android Activity Lifecycle

此外,您的设置文件不是一个良好的偏好实施。除非您希望1个应用程序读取另一个Android应用程序的设置,否则您应该查看SharedPreferences以存储应用程序范围的设置 - 它非常易于使用。

如果您想要一个简单的用户界面,并且想要快速完成,那么会为设置用户界面特别设计一项特殊活动:PreferenceActivity - 虽然稍后您可能会发现它缺少一些功能。