Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何保存切换按钮的状态_Android_Save_Toggle - Fatal编程技术网

Android 如何保存切换按钮的状态

Android 如何保存切换按钮的状态,android,save,toggle,Android,Save,Toggle,我需要保存有关切换按钮的信息,当用户关闭应用程序或重新启动时,我想显示按钮的先前状态,谢谢 public class MainActivity extends ActionBarActivity { ToggleButton toggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我需要保存有关切换按钮的信息,当用户关闭应用程序或重新启动时,我想显示按钮的先前状态,谢谢

 public class MainActivity extends ActionBarActivity {
    ToggleButton toggle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android.support.v7.app.ActionBar menu = getSupportActionBar();
        menu.setDisplayShowHomeEnabled(true);
        menu.setLogo(R.mipmap.ic_launcher);
        menu.setDisplayUseLogoEnabled(true);

    }
    @Override
    protected void onResume() {
        super.onResume();
        toggle = (ToggleButton) findViewById(R.id.togglebutton);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    enableNotifications();
                } else {
                    makeDialog();
                }
            }
        });
    }

使用相同的共享首选项

togButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                     int notification_status = Integer.parseInt(mPreferences.getString("status", "-1"));
                    SharedPreferences.Editor editor=mPreferences.edit();
                    Log.i("notification_status="+notification_status,"888");
                    if(notification_status==-1){
                          editor.putString("status", "1");
                          holder.notification_btn.setBackgroundResource(R.drawable.notification_on);
                          //do your stuff
                    }else if(notification_status==1){
                        editor.putString("status", "0");
                        holder.notification_btn.setBackgroundResource(R.drawable.notification_off);
                        //do your stuff
                    }else if(notification_status==0){
                        editor.putString("status", "1");
                        holder.notification_btn.setBackgroundResource(R.drawable.notification_on);
                        //do your stuff
                    }
                    editor.commit();
                }
            });

使用相同的共享首选项

togButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                     int notification_status = Integer.parseInt(mPreferences.getString("status", "-1"));
                    SharedPreferences.Editor editor=mPreferences.edit();
                    Log.i("notification_status="+notification_status,"888");
                    if(notification_status==-1){
                          editor.putString("status", "1");
                          holder.notification_btn.setBackgroundResource(R.drawable.notification_on);
                          //do your stuff
                    }else if(notification_status==1){
                        editor.putString("status", "0");
                        holder.notification_btn.setBackgroundResource(R.drawable.notification_off);
                        //do your stuff
                    }else if(notification_status==0){
                        editor.putString("status", "1");
                        holder.notification_btn.setBackgroundResource(R.drawable.notification_on);
                        //do your stuff
                    }
                    editor.commit();
                }
            });

使用共享首选项,如下所示

public class YourPreferenes {

    private Context mContext;
    private SharedPreferences mPrefs;
    private static YourPreferenes mYourPreferenes;
    private YourPreferenes(Context ctx){
        mContext = ctx;
        mPrefs = mContext.getSharedPreferences("ATSPreferenes", Context.MODE_PRIVATE);
    }

    public static ATSPreferenes getInstance(Context ctx){
        if(mYourPreferenes == null){
            mYourPreferenes = new YourPreferenes(ctx);
        }
        return mYourPreferenes;
    }

    public void setButtonState(boolean btnstate){
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean("btnstate", btnstate);
        editor.commit();
    }

    public boolean getButtonState(){
        return mPrefs.getBoolean("btnstate", false);
    }
}


And get your saved togalbutton stat even you kill your app.  

使用共享首选项,如下所示

public class YourPreferenes {

    private Context mContext;
    private SharedPreferences mPrefs;
    private static YourPreferenes mYourPreferenes;
    private YourPreferenes(Context ctx){
        mContext = ctx;
        mPrefs = mContext.getSharedPreferences("ATSPreferenes", Context.MODE_PRIVATE);
    }

    public static ATSPreferenes getInstance(Context ctx){
        if(mYourPreferenes == null){
            mYourPreferenes = new YourPreferenes(ctx);
        }
        return mYourPreferenes;
    }

    public void setButtonState(boolean btnstate){
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean("btnstate", btnstate);
        editor.commit();
    }

    public boolean getButtonState(){
        return mPrefs.getBoolean("btnstate", false);
    }
}


And get your saved togalbutton stat even you kill your app.  

您可以使用共享首选项,Android为您提供了一个共享首选项。共享首选项是实现这种简单存储的方法。您可以使用共享首选项存储按钮的切换状态,并在需要时重新填充。答案是,您可以使用共享首选项Android提供了一个解决方案。共享首选项是实现这种简单存储的方法。您可以使用共享首选项来存储按钮的切换状态,并在需要时重新填充。答案在这里