java Android - 如何访问在 onResume 中的 onCreate 中实例化的 View 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4716482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:41:15  来源:igfitidea点击:

Android - How can I access a View object instantiated in onCreate in onResume?

javaandroidview

提问by Chris

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

在我的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 ImageButtonwith something like:

在 中onResume,我希望能够使用以下内容更改 的属性ImageButton

@Override
protected void onResume() {
    super.onResume();
    ib.setImageURI(selectedImageUri);
}

But onResumedoesn't have access to the ib ImageButtonobject. 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.

onResume无权访问该ib ImageButton对象。如果这是一个变量,我会简单地将其设为类变量,但 Android 不允许您在类中定义 View 对象。

Any suggestions on how to do this?

关于如何做到这一点的任何建议?

回答by Captain Spandroid

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:

我会将图像按钮设为实例变量,然后您可以根据需要从这两种方法中引用它。IE。做这样的事情:

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 中相对昂贵,因此如果只在一个地方使用,在方法中使用局部变量会更有效。

回答by Mark B

findViewById()does not create the view, it is just finding the already created view. It was created by inflating the layout R.layout.layout_postin the previous line.

findViewById()不创建视图,它只是找到已经创建的视图。它是通过扩大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 ibto an instance variable so that it is accessible in methods other than onCreate().

您可以简单地调用findViewById()您的onResume()方法以在该方法中获取对它的引用,或者您可以更改ib为一个实例变量,以便在onCreate().

回答by William Scott

Move the declaration from the method to the class. Assuming selectedImageUriis 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);
    }
}