简体   繁体   English

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

[英]Android checkbox checked in one activity and then button appears in another activity

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' 想象一下,有2个活动,'活动A'和'活动B'。'活动A'保持一个复选框,当检查按钮应该显示在'活动B'时,如果未选中该按钮应该隐藏在'活动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' 下面是下一个活动,即'活动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 下面是单击复选框时我想要显示的按钮的XML代码

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

send boolean value with intent bundle in activity B. if it is true show button or hide it. 在活动B中使用intent bundle发送boolean值。如果是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);



                }
            });
        }

send with intent 有意发送

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

handle that on Acitivity B 在Acitivity 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);
    }


}
}

Change your Activity 'A' Like this : 更改您的活动'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);
}

And get bundle extra value in Activity 'B' 并在活动'B'中获得捆绑额外的价值

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

在复选框的保存状态中使用布尔isChecked'活动A',在'活动B'上传递它,通过在'活动B'中使用该布尔集可见性到您的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下按钮后计算已选中复选框的百分比,并在另一个活动中显示百分比(android) - Calculate percentage of checked checkbox after press a button and show percentage in another activity (android) 需要将 RecyclerView 中选中的复选框从一个活动发送到另一个活动 - Need to send the checked checkbox in RecyclerView from one activity to another Android:通过“主要活动”按钮确定在列表视图中选中了哪个复选框 - Android : Determine which checkbox is checked in listview from the Main activity button 如何在android的主要活动中声明另一个活动的按钮? - How to declare a button from another activity in the main one in android? 从Android中的另一个活动获取复选框变量 - Get a checkbox variable from another activity in Android Android-如何在更改活动后保存复选框状态(已选中/未选中) - Android - How to save checkbox state (checked/unchecked) after changing activity 一个活动中的按钮与另一个活动中的按钮相同 - Button in one activity to do the same as button in another activity 发送复选框到另一个活动 - Send checkbox to another Activity 如何设置从其他活动中选中的单选按钮? - How to set radio button checked from another activity? 如何检查另一个活动中选中了哪个单选按钮,然后在IF语句中使用? - How to check which radio button is checked in another activity, and then use in IF Statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM