我的onCreate方法设置内容视图,并设置了一个图像按钮:Android:我如何参考主要活动之外的布局项目?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton mGetClickTime = (ImageButton) findViewById(R.id.clicker);
}
但我希望创建一个改变的ImageButton背景的方法:
public void mUpdateBackground() {
if (backgroundPic) {
randomImageId = R.drawable.bg1;
}
else {
randomImageId = R.drawable.bg0;
}
mGetClickTime.setImageResource(randomImageId);
}
问题是,mUpdateBackground方法不知道布局。如果我在方法中声明布局,它会重置MainActivity中编程方式所做的所有更改。
任何想法如何我可以解决这个问题?
感谢您的帮助。我试过了,但mUpdateBackground方法中的mGetClickTime显示为空,即使onCreate中的一个显示为正确分配。有任何想法吗? –
已经明白了。问题是我忘了更改ImageButton mGetClickTime =(ImageButton)findViewById(R.id.clicker);到mGetClickTime =(ImageButton)findViewById(R.id.clicker);非常感谢 –