[英]Android - How can I access a View object instantiated in onCreate in onResume?
In my onCreate()
method, I'm instantiating an ImageButton
View:在我的
onCreate()
方法中,我正在实例化一个ImageButton
视图:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_post);
final ImageButton ib = (ImageButton) findViewById(R.id.post_image);
...
In onResume
, I want to be able to change the properties of the ImageButton
with something like:在
onResume
,我希望能够使用以下内容更改ImageButton
的属性:
@Override
protected void onResume() {
super.onResume();
ib.setImageURI(selectedImageUri);
}
But onResume
doesn't have access to the ib ImageButton
object.但是
onResume
无权访问ib ImageButton
对象。 If this were a variable, I'd simple make it a class variable, but Android does not allow you to define View object in the class.如果这是一个变量,我会简单地将其设为类变量,但 Android 不允许您在类中定义 View 对象。
Any suggestions on how to do this?关于如何做到这一点的任何建议?
I would make the image button an instance variable, then you can refer to it from both methods if you like.我会将图像按钮设为实例变量,然后您可以根据需要从这两种方法中引用它。 ie.
即。 do something like this:
做这样的事情:
private ImageButton mImageButton = null;
public void onCreate(Bundle savedInstanceState) {
Log.d(AntengoApplication.LOG_TAG, "BrowsePicture onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_post);
mImageButton = (ImageButton) findViewById(R.id.post_image);
//do something with mImageButton
}
@Override
protected void onResume() {
super.onResume();
mImageButton = (ImageButton) findViewById(R.id.post_image);
mImageButton.setImageURI(selectedImageUri);
}
It's worth bearing in mind though that instance variables are relatively expensive in Android, so it's more efficient to use a local variable within the method if it's only used in one place.值得记住的是,尽管实例变量在 Android 中相对昂贵,因此如果只在一个地方使用,在方法中使用局部变量会更有效。
findViewById()
does not create the view, it is just finding the already created view. findViewById()
不创建视图,它只是查找已经创建的视图。 It was created by inflating the layout R.layout.layout_post
in the previous line.它是通过扩大上一行中的布局
R.layout.layout_post
来创建的。
You could simply call findViewById()
in your onResume()
method to get a reference to it in that method, or you could change ib
to an instance variable so that it is accessible in methods other than onCreate()
.您可以简单地在
onResume()
方法中调用findViewById()
以在该方法中获取对它的引用,或者您可以将ib
更改为实例变量,以便在onCreate()
以外的方法中可以访问它。
Move the declaration from the method to the class.将声明从方法移动到类。 Assuming
selectedImageUri
is within scope...假设
selectedImageUri
在范围内......
public class MyApp extends Activity {
ImageButton ib;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(AntengoApplication.LOG_TAG, "BrowsePicture onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_post);
ib = (ImageButton) findViewById(R.id.post_image);
}
@Override
protected void onResume() {
super.onResume();
ib.setImageURI(selectedImageUri);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.