更新后,我尝试其他选项,我在YouTube上观看了具有3种方式执行此操作的视频,直到我添加逻辑为止:
使用以下代码:
public void onClick(final View imageView) {
ImageView mypic1 = (ImageView) findViewById(R.id.select1);
ImageView mypic2 = (ImageView) findViewById(R.id.select2);
ImageView mypic3 = (ImageView) findViewById(R.id.select3);
ImageView mypic4 = (ImageView) findViewById(R.id.select4);
ImageView mypic5 = (ImageView) findViewById(R.id.select5);
ImageView mypic6 = (ImageView) findViewById(R.id.select6);
View.OnClickListener listener= new OnClickListener() {
public void onClick(View v) {
if(imageView instanceof ImageView ){
imageView.setTag(1);
if(imageView.getTag().equals(1)){
((ImageView) imageView).setImageResource(R.drawable.old);
imageView.setTag(2);
}else{
((ImageView) imageView).setImageResource(R.drawable.new);
imageView.setTag(1);
}
}
}};
mypic1.setOnClickListener(listener);
mypic2.setOnClickListener(listener);
mypic3.setOnClickListener(listener);
mypic4.setOnClickListener(listener);
mypic5.setOnClickListener(listener);
mypic6.setOnClickListener(listener);
}
全部编译但不起作用,如果我仅添加烤面包,则可以工作(仅带有烤面包文本的消息)
答案 0 :(得分:0)
像这样修改点击
public void onClick (View v){
switch (v.getId()) {
case R.id.mypic1:
//do you action against mypic1:
break;
case R.id.mypic2:
//do you action against mypic2:
break;
case R.id.mypic3:
//do you action against mypic3:
break;
case R.id.mypic4:
//do you action against mypic4:
break;
case R.id.mypic5:
//do you action against mypic5:
break;
case R.id.mypic6:
//do you action against mypic6:
break;
}
});
答案 1 :(得分:0)
在“片段的活动”的onStart()
中,其中我已添加ImageView,并添加了以下代码。
override fun onStart() {
//get all the image view like this
ImageView mypic1 = (ImageView) findViewById(R.id.your_imageView_id1)
ImageView mypic2 = (ImageView) findViewById(R.id.your_imageView_id2)
ImageView mypic3 = (ImageView) findViewById(R.id.your_imageView_id3)
ImageView mypic4 = (ImageView) findViewById(R.id.your_imageView_id4)
.
.
.
.
//Set onCLickListener like this
View.OnClickListener listener= new OnClickListener() {
public void onClick(View imageView) {
if(imageView instanceof ImageView ){
//add all business logic related to your tag and replacing the image of the view
if(imageView.getTag().equals(1)){
imageView.setImageResource(R.drawable.picprevius);
imageView.setTag(2);
}else{
imageView.setImageResource(R.drawable.newpic);
imageView.setTag(1);
}
}
}
}
//add listener to all the image View like this
mypic1.setOnClickListener(listener)
mypic2.setOnClickListener(listener)
mypic3.setOnClickListener(listener)
mypic4.setOnClickListener(listener)
.
.
.
.
}
答案 2 :(得分:0)
您首先将这一行添加为课程
公共类测试实现了View.OnClickListener { ImageView mypic; ...}
您必须将var类型设置为强制转换
mypic =(ImageView)findViewById(R.id.mypic1);
mypic.setOnClickListener(this);
public void onClick (View v){
switch (v.getId()) {
case R.id.mypic1:
if(mypic.getTag().toString().equals(1)){
mypic.setTag(2);
if (Build.VERSION.SDK_INT >= 21)
mypic.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.newpic));
else
mypic.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.newpic,null));
}else{
if (Build.VERSION.SDK_INT >= 21)
mypic.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.oldpic));
else
mypic.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.oldpic,null));
mypic.setTag(1);
}
break;
case R.id.pic2:
//do you action against mypic2:
break;
case R.id.pic3:
//do you action against mypic3:
break;
case R.id.pic4:
//do you action against mypic4:
break;
case R.id.pic5:
//do you action against mypic5:
break;
case R.id.pic6:
//do you action against mypic6:
break;
}
}