在一个活动中选中Android复选框,然后在另一个活动中显示按钮 [英] Android checkbox checked in one activity and then button appears in another activity
本文介绍了在一个活动中选中Android复选框,然后在另一个活动中显示按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题说的一切。想象一下,有两个活动,活动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 );
}
});
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 layout.bubble_layout,null);
bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener(){
@Override
public void onBubbleRemoved(BubbleLayout bubble){
finish();
System.exit );
}
});
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);
}
下面是下一个活动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){
//显示按钮
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/>
解决方案
MainActivity扩展了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 );
}
});
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);
// 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 );
}
});
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){
//显示按钮
fbbutton1.setVisibility(View.VISIBLE);
}
}
}
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" />
解决方案
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);
}
}
}
这篇关于在一个活动中选中Android复选框,然后在另一个活动中显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文