android - 如何在两个 Activity 之间同步 CheckBox 状态?

标签 android layout checkbox synchronization

我有两个 Activity ,都有相似的布局,即复选框。我想同步两个 Activity 中复选框的状态。我该怎么做?

Settings.class

    package com.example.myapp

    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    public class Settings extends Activity {

    CheckBox checkBox_one  = null;
    CheckBox checkBox_two = null;
    CheckBox checkBox_three = null;
    CheckBox checkBox_four = null;
    CheckBox checkBox_five = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

          //SAVE CHECKBOX STATE//

          checkBox_one = (CheckBox) findViewById(R.id.checkBox1);

          boolean isChecked = getBooleanFromPreferences("isChecked");
          Log.i("start",""+isChecked);
          checkBox_one.setChecked(isChecked);
          //checkBox_one.setChecked(true);//Enable By Default
          checkBox_one.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                Log.i("boolean",""+isChecked);
                Settings.this.putBooleanInPreferences(isChecked,"isChecked");
            }
          });

          checkBox_two = (CheckBox) findViewById(R.id.checkBox2);

          boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
          checkBox_two.setChecked(isCheckedTwo );
          //checkBox_two.setChecked(true);//Enable By Default
          checkBox_two.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
            }
          });

          checkBox_three = (CheckBox) findViewById(R.id.checkBox3);

          boolean isCheckedThree = getBooleanFromPreferences("isCheckedThree");
          checkBox_three.setChecked(isCheckedThree );
          checkBox_three.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Settings.this.putBooleanInPreferences(isChecked,"isCheckedThree");
            }
          });

          checkBox_four = (CheckBox) findViewById(R.id.checkBox4);

          boolean isCheckedFour = getBooleanFromPreferences("isCheckedFour");
          checkBox_four.setChecked(isCheckedFour );
          //checkBox_four.setChecked(true);//Enable By Default
          checkBox_four.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Settings.this.putBooleanInPreferences(isChecked,"isCheckedFour");
            }
          });

          checkBox_five = (CheckBox) findViewById(R.id.checkBox5);

          boolean isCheckedFive = getBooleanFromPreferences("isCheckedFive");
          checkBox_five.setChecked(isCheckedFive );
          checkBox_five.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {

                Settings.this.putBooleanInPreferences(isChecked,"isCheckedFive");
            }
          });

        }

        public void putBooleanInPreferences(boolean isChecked,String key){
        SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, isChecked);
        editor.commit();        
        }
        public boolean getBooleanFromPreferences(String key){
        SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
        Boolean isChecked = sharedPreferences.getBoolean(key, false);
        return isChecked;       

        }
        //-------------------------//


        @Override
        public void onBackPressed()
        {
            // Stop back button Functioning
        }

        public void openrate1(View view) { 
        Intent intent = new Intent(this, Rate.class);
        startActivity(intent);
        }

        public void gotohome(View view) { 
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        }

        public void savesettings(View view) { 
            Toast toast=Toast.makeText(this, "Settings successfully saved!", Toast.LENGTH_LONG); 
            toast.show();   
            }                       

        }

Progress.class

package com.example.myapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;

public class Progress extends Activity {
    ProgressBar progressBar1;
    ProgressBar progressBar2;
    CheckBox checkBox1;
    CheckBox checkBox2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);

        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateProgressBars();
            }
        });

        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateProgressBars();
            }
        });
        }

        public void updateProgressBars() {

          progressBar1.setVisibility(View.GONE);
          progressBar2.setVisibility(View.GONE);

         if (checkBox1.isChecked() && checkBox2.isChecked()) {
            progressBar2.setVisibility(View.VISIBLE);
         } else if (checkBox1.isChecked()) {
            progressBar1.setVisibility(View.VISIBLE);
         }



    }


}

最佳答案

您可以为此使用SharedPreferences。在每个 ActivityOnCreate() 方法中。您可以检查 SharedPreferences 的值是否执行。如果它执行,则根据它设置 CheckBox 的状态。 您可以在 CheckBox 事件监听器 OnCheckedChangeListener() 上设置 SharedPreference 值。

关于android - 如何在两个 Activity 之间同步 CheckBox 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074120/

相关文章:

Android:如何对齐底部的按钮和上方的 ListView ?

javascript - 右键单击自定义菜单无法正常工作

java - 如何在按回或打开 Activity 时隐藏滑动菜单

android - ListView 标题忽略 layout_height

Android org.json.JSONException : Value of type java. lang.String无法转换为JSONObject

java - 如何在 Textview 中写入突出显示的文本

html - Z-index 问题不知道现在该怎么办

ruby-on-rails - 从局部添加内容到布局

android - ListView 中的复选框自动检查最后位置的问题

wpf - 如何将命令绑定(bind)到复选框