获取视图片段的FragmentActivity [英] Get view of Fragment from FragmentActivity
本文介绍了获取视图片段的FragmentActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 FragmentActivity
,我添加一个片段
。
根据一些情况,我想访问按钮
从他们那片段的布局和变化的点击侦听器。
I have a FragmentActivity
where I add a Fragment
.
Depending on some situation, I want to access Buttons
from that fragment's layout and change click listeners of them.
我做的是:
View view = myFragment.getView();
Button myButton = (Button) view.findViewById(R.id.my_button);
myButton.setOnClickListener(new MyClickListener());
但视图
变量总是空
。
我怎样才能获得活动的看法?
How can I get the view of the activity?
推荐答案
如果您要访问你的组件,并设置一些数据,我建议你做这样的片段里面的方法:
If you want to access your component and set some data, I suggest you to make a method inside the fragment like this:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}
public void setSettings(){
Button button = (Button) getView().findViewById(R.id.my_button);
button.setOnClickListener(new MyClickListener());
}
}
这篇关于获取视图片段的FragmentActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文