链接到里面片段查看 [英] linking to View inside fragment
问题描述
我在Android的结果初学者
我下面讲片段结果在互联网上的一课
这个想法是,你有两个片段:结果
一种含按钮,另含TextView的结果
在教训他挂在搜索按钮和文本视图
onActivityCreated(...){...}
I am beginner in android
I was following a lesson on the internet about Fragment
the idea is that you have two fragments:
one containing button the other containing textview
in the lesson the he linked to the button and text view under
onActivityCreated(...){...}
不过,我也做了同样的任务如下(按钮样品)
But I did the same task as follow(button sample)
public class Fragment1 extends Fragment {
Communicator comm;
Button button;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.fr1_layout, container, false);
button=(Button) view.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
comm.respond("Button was clickede");
}
});
return view;
}
和它为我工作
这做法是正确的。
推荐答案
onCreateView()
是为了返回与片段,没有进一步的逻辑关联的视图。
onCreateView()
is meant to return the view associated with the fragment, no further logic.
onActivityCreated()
指相关活动已经完成了它的方法的onCreate()。
onActivityCreated()
means that the associated activity has completed its method onCreate().
您应该实施 onActivityCreated你的逻辑()
。你可以通过调用访问虚增布局 getView()
。
You should implement your logic in onActivityCreated()
. You can access the inflated layout by calling getView()
.
button=(Button) getView().findViewById(R.id.button1);
另见: http://developer.android.com/reference/android /app/Fragment.html (生命周期)
这篇关于链接到里面片段查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!