找到点击的图像视图

时间:2015-11-02 00:21:16

标签: android tags onclicklistener

enter image description here我创建了一个taglayout,它使用内部字符串生成动态标记,类似于stackoverflow的标记。因此,通过按钮click = add和图像click = delete完成从列表中添加和删除。当我用按钮创建一个标签时我可以通过按下旁边的“x”删除它而没有问题。但是当它添加到列表时,我不知道哪个图像视图被按下,所以我可以删除。我发现我不能得到任何其他方式按下的图像的ID?

 adding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == adding) {

                    // Read the edit text
                    stuff = interest.getText().toString().trim();

                    // Inflate the tag layout
                    LayoutInflater layoutInflater = getLayoutInflater();
                    final ViewGroup root = (ViewGroup) findViewById(R.id.tagLayout);

                    final View tagView = layoutInflater.inflate(R.layout.tag_layout, root, false);
                    root.addView(tagView);
                    // Get access to the subviews of Tag View
                    final TextView tagTextView = (TextView) tagView.findViewById(R.id.tagTextView);
                    imgFavorite = (ImageView) tagView.findViewById(R.id.imageView);


                    tagTextView.setText(stuff);

                    groupedInterests.add(stuff);


                    imgFavorite.setOnClickListener(
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View b) {
                                    //remove the tag
                                    ((ViewGroup) tagView.getParent()).removeView(tagView);

                                  //FIND OUT  WHICH IMAGE VIEW WAS PRESSED SO I CAN REMOVE THE STRING FROM THE LIST 


                                }
                            });
                    Log.i(TAG, "first id given" + id);

                     id = id + 1;
                    interest.setText("");
                }
            }

        });

1 个答案:

答案 0 :(得分:2)

您可以将stuff字符串设置为tagView上的标记。

tagView.setTag(stuff);

然后在onClick上你可以得到标签

String textToRemove = (String) tagView.getTag();
// Remove string from list

或者,您应该让代码能够访问tagTextView点击监听器中的imgFavorite

imgFavorite.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View b) {
            //remove the tag
            ((ViewGroup) tagView.getParent()).removeView(tagView);

            //FIND OUT  WHICH IMAGE VIEW WAS PRESSED SO I CAN REMOVE THE STRING FROM THE LIST
            String stringToRemove = tagTextView.getText();
        }
    });