查找动态创建的选定 img 的字符串值
Find the String value of a selected img created dynamically
好的,我已经动态创建了图像。图片是随机的。我想要做的是确定 img 的字符串名称是什么,因为我想将字符串名称与我动态创建的内容进行比较。这必须在 onTouch 上实现。这可能吗谢谢
我希望我的描述足够了。
下面是我的代码
Random rQuestion = new Random();
int iQues = rQuestion.nextInt(numQues - 1)+1;
corrImg = corrAnsIMPID[iQues]; //selectedImg has the string I want to compare
ArrayList<String> quesList = new ArrayList<>();
quesList.add(corrAnsIMPID[iQues]);
quesList.add(myShuffledArray[rQuestion.nextInt(numQues - 1) + 1]);
quesList.add(myShuffledArray[rQuestion.nextInt(numQues - 1) + 1]);
quesList.add(myShuffledArray[rQuestion.nextInt(numQues - 1) + 1]);
Collections.shuffle(quesList);
String[] ranQues = new String[quesList.size()];
ranQues = quesList.toArray(ranQues);
for(String s : ranQues); //contains the 4 string name of the img
//set 4 imgs
ImageView newView1 = new ImageView(this);
int id1 = getResources().getIdentifier(ranQues[0], "drawable", getPackageName());
newView1.setImageResource(id1);
//...repeated 4 times
public boolean onTouch (View v, MotionEvent ev)
{
if (mLongClickStartsDrag) return false;
boolean handledHere = false;
final int action = ev.getAction();
// In the situation where a long click is not needed to initiate a drag, simply start on the down event.
if (action == MotionEvent.ACTION_DOWN) {
handledHere = startDrag (v);
if (v != selectedImg mSpot2.setDragLayer (null); //I want to compare if the selected img = corrImg. this would activate/deactivate the drop zone
}
您可以从其 ID 中获取 resource
的名称。
String name = context.getResources().getResourceEntryName(imageResID);
再次从imageview
获取资源id
先设置一个标签
imageView.setTag(R.drawable.yourDrawable);
然后从该标签中检索
int resourceID = (int) imageView.getTag();
好的,我已经动态创建了图像。图片是随机的。我想要做的是确定 img 的字符串名称是什么,因为我想将字符串名称与我动态创建的内容进行比较。这必须在 onTouch 上实现。这可能吗谢谢
我希望我的描述足够了。
下面是我的代码
Random rQuestion = new Random();
int iQues = rQuestion.nextInt(numQues - 1)+1;
corrImg = corrAnsIMPID[iQues]; //selectedImg has the string I want to compare
ArrayList<String> quesList = new ArrayList<>();
quesList.add(corrAnsIMPID[iQues]);
quesList.add(myShuffledArray[rQuestion.nextInt(numQues - 1) + 1]);
quesList.add(myShuffledArray[rQuestion.nextInt(numQues - 1) + 1]);
quesList.add(myShuffledArray[rQuestion.nextInt(numQues - 1) + 1]);
Collections.shuffle(quesList);
String[] ranQues = new String[quesList.size()];
ranQues = quesList.toArray(ranQues);
for(String s : ranQues); //contains the 4 string name of the img
//set 4 imgs
ImageView newView1 = new ImageView(this);
int id1 = getResources().getIdentifier(ranQues[0], "drawable", getPackageName());
newView1.setImageResource(id1);
//...repeated 4 times
public boolean onTouch (View v, MotionEvent ev)
{
if (mLongClickStartsDrag) return false;
boolean handledHere = false;
final int action = ev.getAction();
// In the situation where a long click is not needed to initiate a drag, simply start on the down event.
if (action == MotionEvent.ACTION_DOWN) {
handledHere = startDrag (v);
if (v != selectedImg mSpot2.setDragLayer (null); //I want to compare if the selected img = corrImg. this would activate/deactivate the drop zone
}
您可以从其 ID 中获取 resource
的名称。
String name = context.getResources().getResourceEntryName(imageResID);
再次从imageview
获取资源id
先设置一个标签
imageView.setTag(R.drawable.yourDrawable);
然后从该标签中检索
int resourceID = (int) imageView.getTag();