如何在两个活动之间同步CheckBox状态?

时间:2014-04-15 03:25:08

标签: android layout checkbox sync

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

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



    }


}

3 个答案:

答案 0 :(得分:1)

您可以使用SharedPreferences。在每个OnCreate()的{​​{1}}方法中。如果执行与否,您可以检查Activity的值。如果执行,则根据它设置SharedPreferences的状态。 您可以在CheckBox事件监听器SharedPreference上设置CheckBox值。

答案 1 :(得分:1)

这是两个活动之间的沟通问题。为此,您可以尝试:

  1. SQLite数据库或共享首选项,如Jatin Dudhat所说。您可以保存活动A中的数据,并从活动B获取数据。
  2. 使用startActivity(Intent),如果要将数据从活动A传递到B,可以在活动A中使用startActvity(Intent),并将intent与数据一起使用并传递给活动B.
  3. 如果您在活动A中启动活动B,并且想要在返回活动A时从活动B获取一些数据,则可以使用startActivityForResult(intent,requestCode);
  4. 您还可以使用广播进行两项活动之间的沟通。
  5. 您可以通过类扩展Application的成员变量来保存数据。
  6. 不太准确,但希望得到一些帮助。

答案 2 :(得分:0)

为什么不将boolean传递给intent.putExtra("checkValue", value);

更具体一点:

在活动A中

我们假设ActivityA extends Activity

private boolean checkState = true; //Let's say you checkbox is currently checked.

public void sendCheckState(){
    Intent intent = new Intent(ActivityA.class, ActivityB.class);
    intent.putExtra("checkValue", value);
    startActivity(intent);
}

现在,您只需在获得sendCheckState()值后致电boolean

在活动B中

您可以在onCreate()内调用此代码(我们仍假设您的ActivityB extends Activity

Intent intent = getIntent();

Bundle extras = intent.getExtras();
if (extras != null) {

    if (extras.containsKey("checkValue")) {
        boolean checkValue = extras.getBoolean("checkValue", false);

        // TODO: Do something with the value.
    }
}