【发布时间】:2013-11-30 23:19:02
【问题描述】:
我正在制作一个程序,当我按下按钮时,这会改变背景颜色,但我有一个问题,当我改变屏幕方向时,这会再次将颜色更改为预定义的颜色,我不知道如何解决这个问题...这是我的代码。
public class MainActivity extends Activity implements OnClickListener {
LinearLayout myLayout;
LinearLayout myLayout2;
// Declare UI elements
private Button firstButton, secondButton, thirdButton, fourthButton, fifthButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Our only layout for this app is main.xml
myLayout = (LinearLayout) findViewById(R.id.myLayout);
myLayout2 = (LinearLayout) findViewById(R.id.myLayout2);
// Initialize the UI components
firstButton = (Button) findViewById(R.id.button1);
// When we creating a button and if we expect that to use for event handling we have to set the listener
firstButton.setOnClickListener(this);
secondButton = (Button) findViewById(R.id.button2);
secondButton.setOnClickListener(this);
thirdButton = (Button) findViewById(R.id.button3);
thirdButton.setOnClickListener(this);
fourthButton = (Button) findViewById(R.id.button4);
fourthButton.setOnClickListener(this);
fifthButton = (Button) findViewById(R.id.button5);
fifthButton.setOnClickListener(this);
}
}
【问题讨论】:
-
你在哪里改变颜色?
-
我有一个开关... case R.id.button1: myLayout.setBackgroundColor(Color.WHITE);
-
当我按下按钮时在屏幕上
-
我的意思是代码在哪里?在 onCreate() 或其他地方设置的 onClickListener 中?
标签: android