hsh*_*hed 10 android android-layout
我正在为线性布局添加子视图.子视图本身在Relativelayout中有一些textview和imageview.单击按钮时,子视图会在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);
}
});
Run Code Online (Sandbox Code Playgroud)
任何帮助或建议将不胜感激.谢谢.
tri*_*ggs 13
只需在创建视图时执行以下操作,即可将自定义标记添加到任何视图
view.setTag(Object o);
Run Code Online (Sandbox Code Playgroud)
然后在onClickListener中找到标签
view.getTag()
Run Code Online (Sandbox Code Playgroud)
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);
}
});
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10487 次 |
| 最近记录: |