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

2024-02-15

这个问题说明了一切。假设有 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" />

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


}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 复选框在一个活动中选中,然后按钮出现在另一活动中 的相关文章

随机推荐