简体   繁体   English

Android,如何将int变量的名称传递给popupwindow?

[英]Android, How to pass the name of int variable to a popupwindow?

How can I pass the name of an int variable to a popupwindow when an image is clicked? 单击图像时,如何将int变量的名称传递给popupwindow? I have set an int per image and I have a lot of images that I had set. 我为每个图像设置了一个整数,并且设置了很多图像。

This is how I'm using the int in a textView on a PopupWindow. 这就是我在PopupWindow上的textView中使用int的方式。

public boolean onLongClick(View v) {
// v.setTag(v);

case R.id.hsv1iv1:
ImageView ivpopup = (ImageView) popupView.findViewById(R.id.pv1);
intcount1++;         // I would like to pass this int name to the popup window. 
break;
case R.id.hsv2iv1:
ImageView ivpopup = (ImageView) popupView.findViewById(R.id.pv1);
intcount2++;         // I would like to pass this int name to the popup window. 
break;

LayoutInflater layoutInflater 
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE); 
View popupView = layoutInflater.inflate(R.layout.popup, null); 
final PopupWindow popupWindow = new PopupWindow(
popupView, 
LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT); 
popupWindow.update(0, 0, 800, 500);
ColorDrawable dw = new ColorDrawable(-005500);
popupWindow.setBackgroundDrawable(dw);
tvpwlikectr = (TextView) popupView.findViewById(R.id.liketv);


Button pwlikebtn =  (Button) popupView.findViewById(R.id.pwlikebtn);

Button btnDismiss = (Button)popupView.findViewById(R.id.cancel);

pwlikebtn.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

intcount1++;
tvpwlikectr.setText(Integer.toString(intcount1));  // this code doesn't work with the intcount1

}});
btnDismiss.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {

popupWindow.dismiss();

popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);

}
 }

Could you explain how you are setting the INT per image? 您能解释一下如何设置每个图像的INT吗? Copying and pasting the code on how you set an INT per image would be helpful, because it's unclear what you mean by you are setting an INT per image. 复制和粘贴有关如何为每个图像设置INT的代码会有所帮助,因为尚不清楚您为每个图像设置INT的含义。

Also, are you interested in the value of the int variable or the name of the variable? 另外,您对int变量的值或变量的名称感兴趣吗? Showing how you are settings lots of images with int per image would help clarify what you are trying to do. 显示您如何设置大量图像(每张图像为int)将有助于阐明您要执行的操作。

-- adding answer after seeing the updated post with code -- -看到更新后的代码后添加答案-

I would create an object that has the name you are interested in (ie intcount1) and an int to keep the actual value. 我将创建一个对象,该对象具有您感兴趣的名称(即intcount1)和一个保留实际值的int。 After that, you can associate each button/ImaveView with that object with the view.setTag method, and get the value via view.getTag method. 之后,您可以使用view.setTag方法将每个按钮/ ImaveView与该对象相关联,并通过view.getTag方法获取值。 Here's an example: 这是一个例子:

private class MyTag {
    String mTagName;
    int mCount;
    MyTag(String tagName) {
       mTagName = tagName;
       mCount = 0;
    }
}

// in your onCreate or initializaion code somewhere
ImageView view1 = (ImageView) popupView.findViewById(R.id.hsv1iv1);
MyTag imageTag = new MyTag("intcount1");
view1.setTag(imageTag);
ImageView view2 = (ImageView) popupView.findViewById(R.id.hsv1iv1);

// this will go wherever you handle the onLongClick
public boolean onLongClick(View v) {
   Object tag = v.getTag();
   if (tag instanceof MyTag) {
      MyTag myTag = (MyTag) tag;
      myTag.mCount++;
   }
}

// I'm assuming you are setting the text from the actual clicked object
// so this will go wherever you are setting the text/handling the click
public void onClick(View v) {
    Object tag = v.getTag();
    if (tag instanceof MyTag) {
       MyTag myTag = (MyTag) tag;
       myTag.mCount++;
       tvpwlikectr.setText(myTag.mTagName);
    }
}   

The bottom line is, creating an object with name/count value, associate each View with its own object using the view.setTag() function, and when you need to read the values, use the view.getTag() to get the object and read the mTagName (the "variable" name) and the mCount (the "variable" value). 最重要的是,创建一个具有名称/计数值的对象,使用view.setTag()函数将每个View与其自己的对象相关联,当您需要读取值时,请使用view.getTag()获取该对象并读取mTagName(“变量”名称)和mCount(“变量”值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM