用吐司的Imageview
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用吐司的Imageview相关的知识,希望对你有一定的参考价值。
我有一个项目。但是当我点击图片时,它会将我重定向到img_3。我希望当我点击时:
img_1 and toast 1
img_2 and toast 2
img_3 and toast 3
并保持相同的图像。
请告诉我,我怎么写代码?
这是代码:
public class MainActivity extends AppCompatActivity {
ImageView view;
Random r;
Button button;
Integer[] images= {
R.drawable.img_1,
R.drawable.img_2,
R.drawable.img_3,
};
int pickedImage = 0,lastPicked = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
r = new Random();
view = (ImageView)findViewById(idImageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
do {
pickedImage = r.nextInt(images.length);
} while (pickedImage == lastPicked);
lastPicked = pickedImage;
view.setImageResource(images[pickedImage]);
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast1 = Toast.makeText(getBaseContext(),"ptak 1", Toast.LENGTH_SHORT);
view.setImageResource(R.drawable.img_1);
toast1.show();
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast2 = Toast.makeText(getBaseContext(),"ptak 2", Toast.LENGTH_SHORT);
view.setImageResource(R.drawable.img_2);
toast2.show();
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast3 = Toast.makeText(getBaseContext(),"ptak 3", Toast.LENGTH_SHORT);
view.setImageResource(R.drawable.img_3);
toast3.show();
}
});
}
}
答案
因为您在单个视图上多次设置OnClick Listener,所以它将覆盖到最后一个的所有其他侦听器。所以这就是toast-3的原因显示了eveytime。
相反,你打电话做这样的事情:
不需要使用do while循环使其复杂化:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickedImage = r.nextInt(images.length);
view.setImageResource(images[pickedImage]);
}
});
在toast之后不需要设置view.setImageResource。
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (pickedImage) {
case 0:
Toast toast1 = Toast.makeText(getBaseContext(), "ptak 1", Toast.LENGTH_SHORT);
toast1.show();
break;
case 1:
Toast toast2 = Toast.makeText(getBaseContext(), "ptak 2", Toast.LENGTH_SHORT);
toast2.show();
break;
case 2:
Toast toast3 = Toast.makeText(getBaseContext(), "ptak 3", Toast.LENGTH_SHORT);
toast3.show();
break;
}
}
});
此代码工作正常(已测试)
单击按钮将获取3中的任意随机图像。
单击图像将为该图像提供Toast。 (像Image-1 to Toast-1)
另一答案
像这样使用自定义Toast
Toast toast = new Toast(this);
toast.setGravity(Gravity.CENTER,100,100);
toast.setView(view);
另一答案
不要三次添加点击监听器。它总是需要最后一个听众。试试如下,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
r = new Random();
view = (ImageView)findViewById(idImageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
do {
pickedImage = r.nextInt(images.length);
} while (pickedImage == lastPicked);
lastPicked = pickedImage;
view.setImageResource(images[pickedImage]);
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(lastPick == 0){
Toast toast1 = Toast.makeText(getBaseContext(),"ptak 1", Toast.LENGTH_SHORT);
view.setImageResource(R.drawable.img_1);
toast1.show();
}else if(lastPick ==1){
Toast toast2 = Toast.makeText(getBaseContext(),"ptak 2", Toast.LENGTH_SHORT);
view.setImageResource(R.drawable.img_2);
toast2.show();
}else if(lastPick ==2){
Toast toast3 = Toast.makeText(getBaseContext(),"ptak 3", Toast.LENGTH_SHORT);
view.setImageResource(R.drawable.img_3);
toast3.show();
}
}
});
}
}
以上是关于用吐司的Imageview的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 中单击 ImageView 时从一个片段移动到另一个片段?