[英]How to know which button is clicked?
There are two buttons in each item of ListView.I want to control the Activity control. ListView的每个项目中都有两个按钮。我想控制Activity控件。 I should operate it in Activity, and can not set the listener to button directly.
我应该在“活动”中对其进行操作,并且不能将侦听器直接设置为按钮。
So I set this code: 所以我设置了这段代码:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
How to know which button of item is clicked? 如何知道单击项目的哪个按钮?
just set different ids for these buttons in xml-layout 只需在xml-layout中为这些按钮设置不同的ID
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
switch (view.getId) {
case R.id.btn1: ...
case R.id.btn2: ...
}
}
You have to handle it in adapter class.It will be most suitable way So make your custom adapter and then try this way in your getView method: 您必须在适配器类中处理它。这将是最合适的方法,因此,请创建您的自定义适配器,然后在getView方法中尝试这种方法:
public View getView(final int position, View convertView,ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = (LinearLayout)inflater.inflate(R.layout.YOUR_LAYOUT, null);
}
Button Button1= (Button) convertView.findViewById(R.id.BUTTON1_ID);
Button Button2= (Button) convertView.findViewById(R.id.BUTTON2_ID);
Button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
}
});
Button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
}
});
return convertView ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.