创建了2个具有相同布局,相同按钮位置,相同按钮ID,一切都相同的活动
示例:
在first_activity和second_activity中添加了3个按钮,所有内容都放在同一个地方,并且具有相同的id,button1和button2,但最后一个按钮,“检查”设置可见性消失。
第一项活动
为button1和button2设置背景资源
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
..................................................
//Change button1 drawable to example1.png
button1 = (Button) ....
button1.setBackgroundResource(R.drawable.example1);
//Change button2 drawable to example1.png
button2 = (Button) ....
button2.setBackgroundResource(R.drawable.example1);
//Check Button
check = (Button) findViewById(R.id.check);
check.setBackgroundResource(R.drawable.example1);
..................................................
}
button1 onClick
public void button1_click (View v){
//If button2 drawable same as check it will changed to another drawable
if (button2.getBackground().getConstantState().equals(check.getBackground().getConstantState())){
button2.setBackgroundResource(R.drawable.example2);
}
else {
button2.setBackgroundResource(R.drawable.example1);
}
}
button2 onClick
public void button2_click (View v){
Intent second_activity=new Intent (getApplicationContext(), second_activity.class);
startActivity(second_activity);
//Calling Second Activity when button2 pressed
finish();
//Finish First Activity
}
条件:首先按下button1,button2 drawable更改为example2.png。第二个按下按钮2将启动second_activity并关闭first_activity
我的问题:如何在启动second_activity后将button2 drawable设置为example2.png
我在second_activity中使用下面的代码与first_activity相同
第二次活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
..................................................
//Change button1 drawable to example1.png
button1 = (Button) ....
button1.setBackgroundResource(R.drawable.example1);
//Change button2 drawable to example1.png
button2 = (Button) ....
button2.setBackgroundResource(R.drawable.example1);
//Check Button
check = (Button) findViewById(R.id.check);
check.setBackgroundResource(R.drawable.example1);
..................................................
}
答案 0 :(得分:1)
您可以向用于启动第二个活动的Intent添加“额外”。这个“额外”可能包含有关按钮状态的信息。在第二个活动的onCreate()
中,您可以检查“额外”中的信息并相应地设置按钮的背景。
编辑:添加示例代码
我会为每个按钮创建一个布尔变量,指示背景的状态并将其添加到每个活动中,如下所示:
private boolean button1Checked;
private boolean button2Checked;
然后,每当您更改背景颜色时,都会更改相应变量的状态:
if (...) {
button2.setBackgroundResource(...);
button2Checked = true; // or false, whatever
}
然后,当你想要启动activity2时,你将2个按钮的状态作为额外内容添加到Intent中,如下所示:
Intent intent=new Intent (this, second_activity.class); // use "this" instead of getApplicationContext()
intent.putExtra("button1Checked", button1Checked);
intent.putExtra("button1Checked", button2Checked);
startActivity(intent);
现在,在第二个活动的onCreate()
中,您可以从传递的附加内容中恢复按钮的状态,如下所示:
button1 = (Button) ....
button1Checked = getIntent().getBooleanExtra("button1Checked", false);
if (button1Checked) {
button1.setBackgroundResource(...); // true state
} else {
button1.setBackgroundResource(...); // false state
}
button2 = (Button) ....
button2Checked = getIntent().getBooleanExtra("button2Checked", false);
if (button2Checked) {
button2.setBackgroundResource(...); // true state
} else {
button2.setBackgroundResource(...); // false state
}
希望你明白这一点。