activity怎么控制fragment中的textview组件
2015-07-17 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464984
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询