activity怎么控制fragment中的textview组件
1个回答
2016-08-17 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
public class ViewPagerFragment extends Fragment {
private Button myButton, btn;
private Callbacks mCallbacks;
private TextView text;
public int i;
public interface Callbacks {
public void addFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.guide_1, container, false);// 关联布局文件
Bundle data = getArguments();
myButton = (Button) rootView.findViewById(R.id.mybutton);// 根据rootView找到button
// 设置按键监听事件
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ViewPagerFragment.this.getActivity(),
"button is click!", Toast.LENGTH_SHORT).show();
}
});
btn = (Button) rootView.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ViewPagerFragment.this.getActivity(), "btn",
Toast.LENGTH_SHORT).show();
mCallbacks.addFragment();
}
});
text = (TextView) rootView.findViewById(R.id.text);
text.setText(1);
return rootView;
}
// 当该Fragment被添加、显示到Activity时,回调该方法
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// 如果Activity没有实现Callbacks接口,抛出异常
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"BookListFragment所在的Activity必须实现Callbacks接口!");
}
// 把该Activity当成Callbacks对象
mCallbacks = (Callbacks) activity;
}
// 当该Fragment从它所属的Activity中被删除时回调该方法
@Override
public void onDetach() {
super.onDetach();
// 将mCallbacks赋为null。
mCallbacks = null;
}
}
private Button myButton, btn;
private Callbacks mCallbacks;
private TextView text;
public int i;
public interface Callbacks {
public void addFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.guide_1, container, false);// 关联布局文件
Bundle data = getArguments();
myButton = (Button) rootView.findViewById(R.id.mybutton);// 根据rootView找到button
// 设置按键监听事件
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ViewPagerFragment.this.getActivity(),
"button is click!", Toast.LENGTH_SHORT).show();
}
});
btn = (Button) rootView.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ViewPagerFragment.this.getActivity(), "btn",
Toast.LENGTH_SHORT).show();
mCallbacks.addFragment();
}
});
text = (TextView) rootView.findViewById(R.id.text);
text.setText(1);
return rootView;
}
// 当该Fragment被添加、显示到Activity时,回调该方法
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// 如果Activity没有实现Callbacks接口,抛出异常
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"BookListFragment所在的Activity必须实现Callbacks接口!");
}
// 把该Activity当成Callbacks对象
mCallbacks = (Callbacks) activity;
}
// 当该Fragment从它所属的Activity中被删除时回调该方法
@Override
public void onDetach() {
super.onDetach();
// 将mCallbacks赋为null。
mCallbacks = null;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询