gpt4 book ai didi

java - Android Studio - 如何更改图像资源?

转载 作者:行者123 更新时间:2023-11-30 01:39:20 24 4
gpt4 key购买 nike

如何更改图片资源?

        final Switch o = (Switch)findViewById(R.id.obamaswitch);
if (o.isChecked()){
o.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
ImageView p = (ImageView)findViewById(R.id.obamahere);
p.???;
}
});

最佳答案

如果你想改变图片资源 onChecked switch 那么你可以这样做

ImageView image = (ImageView)findViewById(R.id.obamahere);
final Switch o = (Switch)findViewById(R.id.obamaswitch);
if (o.isChecked()){
image.setImageResource(R.drawble.icon);
}

如果你想在点击 swith 时设置 ImageResource,那么就这样做

new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
image.setImageResource(R.drawble.icon);
}else{
do your work...
}
}
});

关于java - Android Studio - 如何更改图像资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702172/

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