【问题标题】:How to save the state of toggle button android如何保存切换按钮android的状态
【发布时间】:2015-07-28 14:54:49
【问题描述】:

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

 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();
                }
            }
        });
    }

【问题讨论】:

  • 您可以为此使用共享偏好
  • Android 为此提供了lot of options。共享首选项是这样的简单存储方式。
  • 您可以使用共享首选项来存储按钮的切换状态并在需要时重新填充它
  • 我搞定了答案在这里stackoverflow.com/questions/18899182/…

标签: android save toggle


【解决方案1】:

使用相同的共享首选项。

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();
                }
            });

【讨论】:

    【解决方案2】:

    使用共享偏好作为以下类

    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.  
    

    【讨论】:

      猜你喜欢
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多