-2

我有一个带有 3 个选项卡的选项卡活动,每个选项卡都有自己的片段。第一个选项卡中有一个按钮,我想单击它并导航到另一个选项卡活动(或任何具有片段的活动)。我该如何做到这一点?我正在尝试单击一个按钮并打开一个新活动,但我无法这样做。如果我在学习 android 时得到一些标题,我将不胜感激。这是我的片段类的链接以供参考。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tutorials,container, false);

    Button button1 = (Button) view.findViewById(R.id.introbtn1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent tutorial = new Intent(getActivity(), TutorialIntroduction.class);
            startActivity(tutorial);

            //Tried to create a toast to check if the button works but it doesn't
            //Toast.makeText(getActivity(), "button is clicked!", Toast.LENGTH_LONG).show();
        }
    });

    return view;
}
4

1 回答 1

0

在您的片段中创建一个 ViewPager 变量和一个 setter 方法,当在您的活动中创建片段时调用它(最好在构造函数中执行此操作,但它说您需要它为空)。然后在onClick

ViewPager.setCurrentItem(FragmentPostion)

FragmentPostion 是您要切换到哪个页面的int,在您的情况下为0-2(或1-3不记得了,尝试两个哈哈)。

于 2016-12-19T19:25:48.503 回答