Android checkbox checked in one activity and then

2019-06-17 03:02发布

问题:

The question says it all. Imagine there are 2 activity's, 'Activity A' and 'Activity B'.'Activity A' holds a checkbox when its checked a button should show on 'Activity B' when its unchecked the button should hide on 'Activity B'

below is the main activity

    public class MainActivity extends ActionBarActivity {

    private BubblesManager bubblesManager;
    private boolean isCheckedValue;


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

        initializeBubblesManager();


        final Button add = (Button) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                addNewBubble();
                add.setEnabled(false);
            }
        });

        CheckBox checkBox = (CheckBox)findViewById(R.id.add_fb);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                isCheckedValue = isChecked;

                Intent intent = new Intent(MainActivity.this, PopUpWindow.class);
                intent.putExtra("yourBoolName", isCheckedValue );


            }
        });



    }
 private void addNewBubble() {
        BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
        bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
            @Override
            public void onBubbleRemoved(BubbleLayout bubble) {
                finish();
                System.exit(0);
            }
        });
        bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {

            @Override
            public void onBubbleClick(BubbleLayout bubble) {
                Intent in = new Intent(MainActivity.this, PopUpWindow.class);
                startActivity(in);
            }
        });
        bubbleView.setShouldStickToWall(true);
        bubblesManager.addBubble(bubbleView, 60, 20);
    }

Below is the next activity aka 'activity B'

public class PopUpWindow extends Activity {

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


    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.8),(int)(height*.6));

    Boolean yourBool = getIntent().getBooleanExtra("yourBoolName",false);
    Button fbbutton1 = (Button)findViewById(R.id.fbbutton1);
    if(yourBool){
            //For Displaying Button
        fbbutton1.setVisibility(View.VISIBLE);
    }


}

Below is the XML code for the button I want show when checkbox is clicked

<Button
        android:visibility="gone"
        android:id="@+id/fbbutton1"
        android:onClick="button"
        android:background="@drawable/fbcircle"
        android:layout_width="50dp"
        android:layout_height="50dp" />

回答1:

public class MainActivity extends ActionBarActivity {
private BubblesManager bubblesManager;
private boolean isCheckedValue;


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

    initializeBubblesManager();


    final Button add = (Button) findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            addNewBubble();
            add.setEnabled(false);
        }
    });

    CheckBox checkBox = (CheckBox)findViewById(R.id.add_fb);
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            isCheckedValue = isChecked;
// un-comment this code if you want to go to second activity when check change 
//
//                Intent intent = new Intent(MainActivity.this, PopUpWindow.class);
//                intent.putExtra("yourBoolName", isCheckedValue );
//  startActivity(intent);
        }
    });

}
 private void addNewBubble() {
        BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
        bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
            @Override
            public void onBubbleRemoved(BubbleLayout bubble) {
                finish();
                System.exit(0);
            }
        });
        bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {

            @Override
            public void onBubbleClick(BubbleLayout bubble) {
                Intent in = new Intent(MainActivity.this, PopUpWindow.class);
            in.putExtra("yourBoolName", isCheckedValue );
                startActivity(in);
            }
        });
        bubbleView.setShouldStickToWall(true);
        bubblesManager.addBubble(bubbleView, 60, 20);
    }
}


   public class PopUpWindow extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

setContentView(R.layout.activity_pop_up_window);


DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;
int height = dm.heightPixels;

getWindow().setLayout((int)(width*.8),(int)(height*.6));

Boolean yourBool = getIntent().getBooleanExtra("yourBoolName",false);
Button fbbutton1 = (Button)findViewById(R.id.fbbutton1);
if(yourBool){
        //For Displaying Button
    fbbutton1.setVisibility(View.VISIBLE);
    }


}
}


回答2:

send boolean value with intent bundle in activity B. if it is true show button or hide it.

 //global value
    private boolean isCheckedValue;


    CheckBox checkBox = (CheckBox)findViewById(R.id.chkbox1);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    isCheckedValue = isChecked; //first set value then assign to boolean extra.

Intent intent = new Intent(MainActivity.this, PopUpWindow.class);
            intent.putExtra("yourBoolName", isCheckedValue );
            startActivity(intent);



                }
            });
        }

send with intent

Intent intent = new Intent(this, AcitivityB.class);
intent.putExtra("yourBoolName", isCheckedValue );
startActivity(intent)

handle that on Acitivity B

Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");

if(yourBool){
//display button
}
else{
//hide button
}


回答3:

Change your Activity 'A' Like this :

public class MainActivity extends ActionBarActivity {

private BubblesManager bubblesManager;
private boolean isCheckedValue;
private Intent intent;

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

    initializeBubblesManager();


    final Button add = (Button) findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            addNewBubble();
            add.setEnabled(false);
        }
    });
    intent = new Intent(MainActivity.this, PopUpWindow.class);
    CheckBox checkBox = (CheckBox)findViewById(R.id.add_fb);
    checkBox.setOnCheckedChangeListener  (newCompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            isCheckedValue = isChecked;


            intent.putExtra("yourBoolName", isCheckedValue );


        }
    });



}
  private void addNewBubble() {
    BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
    bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
        @Override
        public void onBubbleRemoved(BubbleLayout bubble) {
            finish();
            System.exit(0);
        }
    });
    bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {

        @Override
        public void onBubbleClick(BubbleLayout bubble) {

            startActivity(intent);
        }
    });
    bubbleView.setShouldStickToWall(true);
    bubblesManager.addBubble(bubbleView, 60, 20);
}

And get bundle extra value in Activity 'B'

 boolean checkedStatus= getIntent().getBooleanExtra("yourBoolName",false);
 if(checkedStatus){
  // Do your job here
 }else{
 }


回答4:

Use boolean isChecked of 'Activity A' in that save state of checkbox, pass it on 'Activity B', By using that boolean set visibility to your view in 'Activity B'.