Android 在一个 activity 中选中了复选框,然后在另一个 activity 中出现了按钮

Android checkbox checked in one activity and then button appears in another activity

问题说明了一切。假设有 2 个 activity、'Activity A' 和 'Activity B'。'Activity A' 持有一个复选框,当它被选中时,一个按钮应该显示在 'Activity B' 上,当它未被选中时应该隐藏在 'Activity B'

下面是主要内容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);
    }

下面是下一个 activity 又名 '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);
    }


}

下面是我想要在单击复选框时显示的按钮的 XML 代码

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

在复选框的保存状态中使用 'Activity A' 的布尔值 isChecked,将其传递给 'Activity B',通过使用该布尔值设置视图在 'Activity B' 中的可见性。

在 activity B 中发送带有意图包的布尔值。如果为真,则显示或隐藏按钮。

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



                }
            });
        }

有意发送

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

在活动 B 上处理

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

if(yourBool){
//display button
}
else{
//hide button
}
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);
    }


}
}

改变你的 Activity 'A' 像这样:

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

并在 Activity 'B'

中获得捆绑包额外价值
 boolean checkedStatus= getIntent().getBooleanExtra("yourBoolName",false);
 if(checkedStatus){
  // Do your job here
 }else{
 }