我是android的新手,我刚刚从android studio的画廊创建了一个Tabbed Activity。
现在我想创建一个按钮,让我从片段的第1页切换到第2页,我该怎么办?
无法找到任何使用"选项卡式活动的示例"安卓工作室。
在Neelay Srivastava的回答之后编辑:
现在是我的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
bn1 = (Button) rootView.findViewById(R.id.b_next1);
if(getArguments().getInt(ARG_SECTION_NUMBER)==1) {
bn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View V)
{
Fragment someFragment = new Replacement(); <--- ERROR HERE
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frag5 , someFragment ); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
}
});
}
我做到了,但我得到了:
无法解析符号&#39;替换&#39;
答案 0 :(得分:2)
要通过按钮将一个片段转到另一个片段,您必须设置一些像这样的东西
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_album, container, false);
Button button =(Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment someFragment = new Replacement();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.replace_frag, someFragment ); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
}
});
return view;
}
此处replacefrag
是您要替换的布局的ID,因此您必须设置布局的ID