【问题标题】:how to save the state of switchcompat in android?如何在android中保存switchcompat的状态?
【发布时间】:2017-04-21 18:48:22
【问题描述】:

我有这个 android 应用程序,其中使用了 switchcompat。我已经在其中尝试了 sharedPreferences() 。我无法保存 switchcompat 的状态。就像当我按下并退出活动时,它会自动关闭。 这是我的代码

package com.example.srushtee.dummy;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.Menu; 
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.google.firebase.messaging.FirebaseMessaging;

public class SettingsActivity extends AppCompatActivity {

private SwitchCompat switchCompat;
private Boolean isChecked=false;
private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    switchCompat=(SwitchCompat) findViewById(R.id.switchButton);
    FirebaseMessaging.getInstance().subscribeToTopic("APP");


    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(switchCompat.isChecked())
            {
                SharedPreferences.Editor editor=getSharedPreferences("com.example.srushtee.dummy",MODE_PRIVATE).edit();
                editor.putBoolean("True",true);
                editor.commit();

                FirebaseMessaging.getInstance().unsubscribeFromTopic("APP");



            }
            else {
                SharedPreferences.Editor editor=getSharedPreferences("com.example.srushtee.dummy",MODE_PRIVATE).edit();
                editor.putBoolean("false",false);
                editor.commit();
                FirebaseMessaging.getInstance().subscribeToTopic("APP");


            }

        }
    });

}

}

请帮忙。提前谢谢你

【问题讨论】:

  • 我看到您正在保存 SharedPreferences。我不知道你是如何使用它的。你能分享那个代码吗?
  • 这是代码。

标签: android switchcompat


【解决方案1】:

您可以使用开关上的setOnClickListener 并保存您的状态..in sharedPreferences

 switchCompat.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View arg0) {    
           SharedPreferences.Editor editor = getSharedPreferences("com.example.srushtee.dummy", MODE_PRIVATE).edit();
           editor.putBoolean("service_status", switchCompat.isChecked());
           editor.commit();
       }
   }

现在使用以下方法检索值:在 onCreate() 中调用它

  SharedPreferences prefs = getSharedPreferences("com.example.srushtee.dummy", MODE_PRIVATE);
  boolean switchState = pref.getBoolean("service_status", false);

  if(switchState){
        //Do your work for switch is selected on
  } else {
        //Code for switch off
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    相关资源
    最近更新 更多