我在我的应用程序中有一个按钮和一个ImageView.我想要做的是当我按下按钮然后ImageView上的图像将改变.我只有两个图片文件.
我想要做的是 - 如果第一张图片链接到ImageView,而不是通过点击按钮将其更改为pic2,如果pic2已链接,则点击该按钮会将其更改回第一张图片文件.
这是我试图使用的onClick方法:
public void onClick(View v) {
ImageView ib1 = (ImageView)findViewById(R.id.imageView1)
View p1 = findViewById(R.drawable.pic1);
if(ib1.getResources()==R.drawable.pic1){
ib1.setImageResource(R.drawable.pic2);
}else{
ib1.setImageResource(R.drawable.pic1);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
Moh*_*hin 10
Rather than checking the image, I would suggest set the information tag of the ImageView each time you change the image, like:
if(ib1.getTag() != null && ib1.getTag().toString().equals("pic1")){
ib1.setImageResource(R.drawable.pic2);
ib1.setTag("pic2");
} else {
ib1.setImageResource(R.drawable.pic1);
ib1.setTag("pic1");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4056 次 |
| 最近记录: |