如何保存两个切换按钮的状态?

时间:2014-04-08 10:08:00

标签: android

我正在制作一个项目,其中我使用两个切换按钮,我必须保存状态,以便它可以在后台工作,因为它是保存与否。 我能够保存第一个togglebutton的状态,但不能保存第二个togglebutton的状态,第二个也可以启动和停止服务按照 。所以任何人都可以告诉我必须做什么,以便我可以保存togglebuttons的状态

...代码

<ToggleButton
        android:id="@+id/tgbattery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/toggleButton1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="45dp"
        android:checked="true"
     />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="113dp"
        android:layout_marginTop="74dp"
        android:checked="true" />


public class MainActivity extends Activity implements OnCheckedChangeListener{

    ToggleButton tb,tb1;
    SharedPreferences shpref,shpref1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tb = (ToggleButton)findViewById(R.id.toggleButton1);
        tb.setOnCheckedChangeListener(this);
        shpref=getSharedPreferences("announcepref", MODE_PRIVATE);
        tb.setChecked(shpref.getBoolean("is announcer enable", true));
        tb1 = (ToggleButton)findViewById(R.id.tgbattery);
        tb1.setOnCheckedChangeListener(this);
        shpref1=getSharedPreferences("batterypref", MODE_PRIVATE);
        tb1.setChecked(shpref1.getBoolean("is battery enable", true));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        Intent i = new Intent(MainActivity.this,BatteryService.class);
        if(tb.isChecked()){
            SharedPreferences.Editor editor=shpref.edit();
            editor.putBoolean("is announcer enable", true);
            editor.commit();
            Toast.makeText(getApplicationContext(), "Call Announcer Activated", Toast.LENGTH_SHORT).show();

        }else if(!tb.isChecked()){
            SharedPreferences.Editor editor=shpref.edit();
            editor.putBoolean("is announcer enable", false);
            editor.commit();
            Toast.makeText(getApplicationContext(), "Call Announcer Deactivated", Toast.LENGTH_SHORT).show();
        }
        else if(tb1.isChecked()){
            SharedPreferences.Editor editor=shpref1.edit();
            editor.putBoolean("is battery enable", true);
            editor.commit();
            Toast.makeText(getApplicationContext(), "Low Battery Alert Activated", Toast.LENGTH_SHORT).show();
            startService(i);
        }
        else if(!tb1.isChecked()){
            SharedPreferences.Editor editor=shpref1.edit();
            editor.putBoolean("is battery enable", false);
            editor.commit();
            stopService(i);
            Toast.makeText(getApplicationContext(), "Low Battery Alert Deactivated", Toast.LENGTH_SHORT).show();
        }
    }

}

2 个答案:

答案 0 :(得分:1)

在onCheckedChanged()中,最后2个 else if 测试将永远不会执行,因为tb.isChecked()将为true或者!tb.isChecked()将为true。

尝试以这种方式简化和修复:

    if (arg0.equals(tb)) {
        if (tb.isChecked()){
            final SharedPreferences.Editor editor=shpref.edit();
            editor.putBoolean("is announcer enable", true);
            editor.commit();
            Toast.makeText(getApplicationContext(), "Call Announcer Activated", Toast.LENGTH_SHORT).show();

        } else {
            final SharedPreferences.Editor editor=shpref.edit();
            editor.putBoolean("is announcer enable", false);
            editor.commit();
            Toast.makeText(getApplicationContext(), "Call Announcer Deactivated", Toast.LENGTH_SHORT).show();
        }
    } else if (arg0.equals(tb1)) {
        if (tb1.isChecked()) {
            final SharedPreferences.Editor editor=shpref1.edit();
            editor.putBoolean("is battery enable", true);
            editor.commit();
            Toast.makeText(getApplicationContext(), "Low Battery Alert Activated", Toast.LENGTH_SHORT).show();
        } else {
            final SharedPreferences.Editor editor=shpref1.edit();
            editor.putBoolean("is battery enable", false);
            editor.commit();
            Toast.makeText(getApplicationContext(), "Low Battery Alert Deactivated", Toast.LENGTH_SHORT).show();
        }
    }

    if (tb1.isChecked()) {
        startService(i);
    } else {
        stopService(i);
    }

答案 1 :(得分:0)

首先检查是否选中了tb1:

if(tb1.isChecked()){
//Then start your service.
}

检查切换如下所示的值后,检查哪个切换按下了

@Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        Intent i = new Intent(MainActivity.this,BatteryService.class);
        if(arg0.equals(tb))
        {
            if(tb.isChecked()){
                SharedPreferences.Editor editor=shpref.edit();
                editor.putBoolean("is announcer enable", true);
                editor.commit();
                Toast.makeText(getApplicationContext(), "Call Announcer Activated", Toast.LENGTH_SHORT).show();

            }else if(!tb.isChecked()){
                SharedPreferences.Editor editor=shpref.edit();
                editor.putBoolean("is announcer enable", false);
                editor.commit();
                Toast.makeText(getApplicationContext(), "Call Announcer Deactivated", Toast.LENGTH_SHORT).show();
            }   
        }
        else if (arg0.equals(tab1))
        {
            if(tb1.isChecked()){
                SharedPreferences.Editor editor=shpref1.edit();
                editor.putBoolean("is battery enable", true);
                editor.commit();
                Toast.makeText(getApplicationContext(), "Low Battery Alert Activated", Toast.LENGTH_SHORT).show();
                startService(i);
            }
            else if(!tb1.isChecked()){
                SharedPreferences.Editor editor=shpref1.edit();
                editor.putBoolean("is battery enable", false);
                editor.commit();
                stopService(i);
                Toast.makeText(getApplicationContext(), "Low Battery Alert Deactivated", Toast.LENGTH_SHORT).show();
            }
        }
    }