【问题标题】:How to get the ID of the clicked child view added dynamically to a LinearLayout?如何获取动态添加到LinearLayout的点击子视图的ID?
【发布时间】:2012-09-25 21:50:03
【问题描述】:

我正在向线性布局添加子视图。子视图本身在相对布局中有一些文本视图和图像视图。 单击按钮时,子视图会动态添加到 LinearLayout 中。现在我可以添加子视图,如图所示。 http://dl.dropbox.com/u/50249620/SC20120926-031356.png 我必须做的是唯一地确定单击了哪个子视图以显示适当的操作。 我在其中添加子视图的代码。

addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


                customView1 = inflater.inflate(R.layout.people, null);

                peopleName = (TextView) customView1.findViewById(R.id.peopleName);

                peopleName.setText(autoComplete.getText());
                customView1.setId(peopleInvitedRelativeLayout.getChildCount() + 1);

                params4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                customView1.setLayoutParams(params4);
                peopleInvitedRelativeLayout.addView(customView1, params4);              

            }
        }); 

任何帮助或建议将不胜感激。谢谢。

【问题讨论】:

  • 这个链接怎么样? stackoverflow.com/questions/7807058/…
  • 如果我知道父LinearLayout中子视图的索引,我可以获得id,但我无法获取用户点击的子视图的索引。
  • onClick() 事件中,点击的View 作为参数传递。您可以在 click 事件中对该视图调用 getId()
  • triggs 我也有类似的疑问。请帮忙:stackoverflow.com/questions/17061833/…

标签: android android-layout


【解决方案1】:

您可以在创建视图时简单地通过执行以下操作向任何视图添加自定义标签

view.setTag(Object o);

然后稍后在 onClickListener 中找到带有

的标签
view.getTag()

setTag(Object o) 将接受任何类型的对象,无论是字符串、int 还是自定义类

编辑

addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


            customView1 = inflater.inflate(R.layout.people, null);

            peopleName = (TextView) customView1.findViewById(R.id.peopleName);

            peopleName.setText(autoComplete.getText());
            customView1.setId(peopleInvitedRelativeLayout.getChildCount() + 1);

            params4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            customView1.setLayoutParams(params4);
            peopleInvitedRelativeLayout.addView(customView1, params4);

            //add a tag to a view and add a clicklistener to the view
            customView1.setTag(someTag);
            customView1.setOnClickListener(myClickListner);



        }
    });

clicklistener - 为它创建一个类变量

OnClickListener myClickListener = new onClickListener(){
    @Override
    public void onClick(View v) {

        if(v.getTag() == someTag){
             //do stuff
        }else if(v.getTag() == otherTag){
             //do something else
        }
    }

【讨论】:

  • 当我使用customview1.setonclicklistener .....时,customview1 为空,因为它已在button.setonclicklistener 中膨胀。因此,我无法确定单击了哪个子视图。
  • 然后将 onClickListener 添加到 addButton onClickListener 中的 customView1,我将编辑我的答案并提供更多详细信息。
  • 感谢您的帮助。它似乎正在工作。我使用过 OnLongClickListener。我必须在长按某个项目时显示上下文菜单。上下文菜单项之一必须删除单击的项,这不适用于我的代码。假设我必须删除第一项 public boolean onContextItemSelected(MenuItem item) { if(item.getTitle()=="Action 1"){ peopleInvitedRelativeLayout.removeViewAt(0); } 否则 {返回假;} 返回真;其中 Action 1 是上下文菜单项。 removeView 的东西不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多