创建了 2 个具有相同布局、相同按钮位置、相同按钮 ID 的活动,一切都相同
例子:
在 first_activity 和 second_activity 中添加了 3 个按钮,所有内容都放置在同一个位置,并且具有相同的 id、button1 和 button2,但最后一个按钮“check”将可见性设置为消失。
第一项活动
为 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。Second Pressed button2 将启动 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);
..................................................
}