Java 如何在Android Studio中将文本设置为相应的图像
我正在开发一个android应用程序,我想在图像视图中设置一个图像,通过单击按钮,我从字符串数组中获得了相应的文本。 我已经把我的图像放在可绘图文件夹中了 字符串的数组为:Java 如何在Android Studio中将文本设置为相应的图像,java,arrays,image,android-studio,if-statement,Java,Arrays,Image,Android Studio,If Statement,我正在开发一个android应用程序,我想在图像视图中设置一个图像,通过单击按钮,我从字符串数组中获得了相应的文本。 我已经把我的图像放在可绘图文件夹中了 字符串的数组为: private String[] captions =new String[] { /*0*/"man in white shirt and glasses is sitting at table", /*1*/"man in black shirt is pla
private String[] captions =new String[] {
/*0*/"man in white shirt and glasses is sitting at table",
/*1*/"man in black shirt is playing the guitar",
/*2*/"man in blue shirt is standing in front of water",
/*3*/"two dogs are running through the snow",
/*4*/"group of people are standing in front of crowd",
/*5*/"two dogs are playing in the snow",
/*6*/"group of people are sitting at the table",
/*7*/"man in blue shirt is standing in front of building",
/*8*/"man in blue shirt is standing in the water of the water",
/*9*/"man in black shirt playing the guitar",
/*10*/"man in black shirt and blue is sitting on the beach",
/*11*/"group of people are standing in front of building",
/*12*/"group of people are standing in the street",
/*13*/"man in white shirt is holding his face",
/*14*/"man in white shirt is cooking",
/*15*/"two men are playing in the grass",
/*16*/"man in black shirt and white is playing the guitar",
};
我可以在这里使用if条件。我在这个项目中使用视图绑定,这就是为什么我访问binding.buttonid这样的按钮
binding.detectCaption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//binding.displayView.setText("two dogs are running through the snow");
if (binding.imageView==R.drawable.one)//This line shows error{
binding.displayView.setText(captions[0]);
}
}
});
请告诉我如何执行此操作。在您的情况下,您正在检查ImageView对象是否等于drawable,这两个对象是不同的 很好的解决方案是,在将drawable设置为imageview时,还可以放置如下标记:
binding.imageView.setTag("Your tag");
我不知道如何设置这些图像,但我假设使用一些整数,所以在这里,在您的标记中,您可以设置图像的整数(“1”表示“one.png”,“2”表示“two.png”等),然后您甚至不需要使用if条件,只需将描述设置为
binding.displayView.setText(captions[(int) binding.imageView.getTag()]);
问题是您试图将
图像视图
与整数
(可绘制的资源id)进行比较
另一个更普遍的问题是,ImageView的id
与其资源(即可绘图)的id不匹配,因此您的条件在任何方面都有缺陷
您可以做的是根据当前可绘制的id设置ImageView的标记
,并基于该id设置您的条件,但这只有在以编程方式设置资源时才有意义,或者如果您确实需要知道当前可绘制的是什么。如果情况并非如此,请使用@Dorian Pavetić的答案
// first set a drawable as example
binding.imageView.setImageResource(R.drawable.one);
// and now give the imageView a tag that matches the resource's id
binding.imageView.setTag(R.drawable.one);
// change the condition:
binding.detectCaption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//binding.displayView.setText("two dogs are running through the snow");
Object tag = binding.imageView.getTag();
if ( tag != null && (int) tag == R.drawable.one) {
binding.displayView.setText(captions[0]);
}
}
});
无法从ImageView获取可绘制id。
因此,您可以使用标记、自定义ImageView或任何其他方式。
为了使用tag,@TimonNetherlands和@Dorian Pavetić已经提到了它
因此,我描述了定制ImageView
public class YourImageView extends ImageView {
private int drawableId;
public YourImageView(Context context) {
super(context);
}
public YourImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public YourImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setImageResource(int resId) {
super.setImageResource(resId);
this.drawableId = resId;
}
public int getResourceId() {
return drawableId;
}
}
在这个方法中,您已经将图像设置为ImageView,但在我的例子中,我设置了来自gallery的图像(与我在drawable文件夹中上载的图像相同)。在这里,我无法获得我通过gallery上传的照片的名称或Id。建议我如何获取这些信息。我使用手机的多媒体资料在ImageView中设置图像。但我无法获得上传图像的信息。建议我如何获得这些信息的方法,以便我可以在if条件下轻松比较。我不确定你到底在寻找什么。下面是如何检索图像名称:之后,您可以设置任何有助于识别标题的标记。在if中使用“getTag()”,而不是ImageView对象