我如何在Fragment中设置setOnClickListener [英] How do i do setOnClickListener in Fragment
本文介绍了我如何在Fragment中设置setOnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问我是android的菜鸟,我刚刚被分配了一个任务,我需要在两天内完成它,
Please i'm noob in android,i just have been assigned a task and i need to finish it in two days,
我需要设置Onclicklistener(this),我从某人那里收到了一个代码,我不得不对其进行更改,我正在尝试在Fragment中实现它
I need to set Onclicklistener(this),i received a code from someone,and i had to make changes to it,i'm trying to implement this in Fragment
View sortableHeaderWrappers[]=new View[]{view.findViewById(R.id.date_wrapper),view.findViewById(R.id.status_wrapper)};
然后有一个写为
for(int i=0;i<sortableHeaderWrappers.length;i++)
{
sortableHeaderWrappers[i].setTag((Integer)i);
sortableHeaderWrappers[i].setOnClickListener(this)
}
但是我无法使用它,我知道您可以使用getContext()代替"this",但是那不能正常工作
But i'm not able to use this,i understand you can use getContext() instead of "this",but thats not working as well
任何输入都会有所帮助
推荐答案
您可以在for循环中初始化新的onClickListener.
You can initialize a new onClickListener in for loop.
for(int i=0;i<sortableHeaderWrappers.length;i++)
{
sortableHeaderWrappers[i].setTag((Integer)i);
sortableHeaderWrappers[i].setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//.. place your logic that needs to be executed when click happens.
}
})
}
这篇关于我如何在Fragment中设置setOnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文