如何将图像按钮作为参数传递给 android studio 中的函数?

How to pass an imagebutton as an argument to a function in android studio?

我的应用程序中有一些图像按钮,它们显示的图像是在代码中确定的(它们不应该响应 touch/click)。如何将图像按钮作为参数传递给 android studio 中的函数?我是这样做的:

private void compute_progress() {
   ...  
   progress_1();
   progress_2();
   ...
   progress_10();
   ...
}

private void progress_1() {
   ImageButton p1 = (ImageButton) findViewById( R.id.Progress1 );
   ...
   p1.setImageResource ( R.drawable.picture );
   ...
}

private void progress_2() {
   ImageButton p2 = (ImageButton) findViewById( R.id.Progress2 );
   ...
   p2.setImageResource ( R.drawable.picture );
   ...
}

...

private void progress_10() {
   ImageButton p10 = (ImageButton) findViewById( R.id.Progress10 );
   ...
   p10.setImageResource ( R.drawable.picture );
   ...
}

但我应该也想这样做:

private void compute_progress() {
   ImageButton p1 = (ImageButton) findViewById( R.id.Progress1 );
   ImageButton p2 = (ImageButton) findViewById( R.id.Progress2 );
   ...
   ImageButton p10 = (ImageButton) findViewById( R.id.Progress10 );

   ...

   progress("imagebutton p1");
   progress("imagebutton p2");
   ...
   progress("imagebutton p10");

   ...
}

private void progress("receive ImageButton p") {
   ...
   p.setImageResource ( R.drawable.picture );
   ...
}

定义您想要接收 ImageButton 作为参数的方法,如下所示:

private void progress(ImageButton imgButton) {
    imgButton.setImageResource(R.drawable.picture)
}

然后当你需要调用你的方法时,像这样调用它(P1 是你的 ImageButton:

progress(p1)