gpt4 book ai didi

java - onClick() 更改图像

转载 作者:行者123 更新时间:2023-12-01 18:45:10 24 4
gpt4 key购买 nike

我正在构建一个 Android 应用程序,我需要一些建议(我对 java 完全陌生,我一直在阅读 this ),我有一个按钮和一张图像,我需要每次更改图像按钮被按下。这是一个看起来像石头的按钮,图像是一个碗,我需要每次按下石头按钮时,碗里都会多一颗石头(我有带有 1, 2, 3, . ..里面有石头)。我该怎么做呢?我不一定要代码,只是我应该这样做。

最佳答案

将碗图像放入数组中

int[] Img_array={R.drawable.bowl1,R.drawable.bowl2,......}

& 在按钮的 Onclick 事件中使用其位置

public class MainActivity extends Activity {
Public int counter=0;
Button button;
ImageView imageview;
int[] Img_array={R.drawable.bowl1,R.drawable.bowl2};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.MyButton);
imageview=(ImageView)findViewById(R.id.MyImageView);
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
imageview.setBackgroundResource(Img_array[counter]);
Counter++;
if(counter==Img_array.length())
{
Counter=0;
}


}
});
}
}

关于java - onClick() 更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18076226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com