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

时间:2016-08-13 11:54:04

标签: java android android-studio checkbox android-button

这个问题说明了一切。想象一下,有2个活动,'活动A'和'活动B'。'活动A'保持一个复选框,当检查按钮应该显示在'活动B'时未选中该按钮应该隐藏在'活动B'上

以下是主要活动

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

下面是下一个活动,即'活动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" />

3 个答案:

答案 0 :(得分:3)

在活动B中使用intent bundle发送布尔值。如果是true,则显示按钮或隐藏它。

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

在Acitivity B上处理

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

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

答案 1 :(得分:3)

NPM WebJar

答案 2 :(得分:1)

更改您的活动&#39; A&#39;像这样:

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

在活动&#39; B&#39;

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