Android的 - 我如何可以访问onResume中的onCreate实例化的视图对象? [英] Android - How can I access a View object instantiated in onCreate in onResume?

查看:189
本文介绍了Android的 - 我如何可以访问onResume中的onCreate实例化的视图对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的onCreate()方法,我实例化一个ImageButton的观点:

In my onCreate() method, I'm instantiating an ImageButton View:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_post);

    final ImageButton ib = (ImageButton) findViewById(R.id.post_image);
...

在onResume,我希望能够与像改变的ImageButton的属性:
@覆盖
保护无效onResume(){
    super.onResume();
    ib.setImageURI(selectedImageUri);
} // END onResume

In onResume, I want to be able to change the properties of the ImageButton with something like: @Override protected void onResume() { super.onResume(); ib.setImageURI(selectedImageUri); } //END onResume

但是onResume不具有访问该IB的ImageButton对象。如果这是一个变量,我会简单使其成为一个类变量,而Android不允许你在类中定义视图对象。

But onResume doesn't have access to the ib ImageButton object. 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.

这是如何做到这一点有什么建议?

Any suggestions on how to do this?

推荐答案

我会使图像按钮的实例变量,那么你可以参考它的两种方法,如果你喜欢。 IE浏览器。做这样的事情:

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);
}

这是值得铭记尽管这实例变量都在Android的相对昂贵,所以它更有效地使用该方法中的局部变量,如果它在一个地方只用。

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的 - 我如何可以访问onResume中的onCreate实例化的视图对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆