Java 所有按钮都从Android片段打开相同的活动

Java 所有按钮都从Android片段打开相同的活动,java,android,android-fragments,Java,Android,Android Fragments,目前,我有一个fragment\u one.xml,上面有5个cardwiews,每张卡片上都有一个按钮,用于转到单独的xml页面(第一课、第二课等),但是有了OneFragment.java中的代码,两个按钮都打开了Lesson\u Two 我怎样才能解决这个问题?这是我的密码 FragmentOne.java 您分配了两次intent,有效地用第二个intent覆盖了第一个intent 因此,无论触发哪个单击事件,类都是启动的活动 一个简单的修复方法是在单击处理程序中创建意图,如 publi

目前,我有一个
fragment\u one.xml
,上面有5个
cardwiews
,每张卡片上都有一个按钮,用于转到单独的xml页面(第一课、第二课等),但是有了
OneFragment.java
中的代码,两个按钮都打开了
Lesson\u Two

我怎样才能解决这个问题?这是我的密码

FragmentOne.java


您分配了两次
intent
,有效地用第二个intent覆盖了第一个intent

因此,无论触发哪个单击事件,类都是启动的活动

一个简单的修复方法是在单击处理程序中创建意图,如

public class OneFragment extends Fragment{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_one, container, false);
    final Button button = (Button) root.findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), LessonOne.class));
        }
    });

    final Button button2 = (Button) root.findViewById(R.id.button2);

    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), LessonTwo.class););
        }
    });

    return root;
  }

}

这就明确了哪个单击处理程序启动了您两次分配
意图的活动,有效地用第二个意图覆盖了第一个意图

因此,无论触发哪个单击事件,类都是启动的活动

一个简单的修复方法是在单击处理程序中创建意图,如

public class OneFragment extends Fragment{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_one, container, false);
    final Button button = (Button) root.findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), LessonOne.class));
        }
    });

    final Button button2 = (Button) root.findViewById(R.id.button2);

    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), LessonTwo.class););
        }
    });

    return root;
  }

}

这将明确哪个单击处理程序启动什么活动

备选答案-在类本身上实现单击侦听器

这将清除
onCreateView
方法。您也不需要“捕获”按钮来设置其侦听器

public class OneFragment extends Fragment implements View.OnClickListener {

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

        root.findViewById(R.id.button1).setOnClickListener(this);
        root.findViewById(R.id.button2).setOnClickListener(this);

        return root;
    }

    @Override
    public void onClick(View v) {
        Class clz = null;
        switch (v.getId()) {
            case R.id.button1:
                clz = LessonOne.class;
            case R.id.button2;
                clz = LessonTwo.class;
        }

        if (clz != null) startActivity(new Intent(getActivity(), clz));

    }

}

备选答案-在类本身上实现click侦听器

这将清除
onCreateView
方法。您也不需要“捕获”按钮来设置其侦听器

public class OneFragment extends Fragment implements View.OnClickListener {

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

        root.findViewById(R.id.button1).setOnClickListener(this);
        root.findViewById(R.id.button2).setOnClickListener(this);

        return root;
    }

    @Override
    public void onClick(View v) {
        Class clz = null;
        switch (v.getId()) {
            case R.id.button1:
                clz = LessonOne.class;
            case R.id.button2;
                clz = LessonTwo.class;
        }

        if (clz != null) startActivity(new Intent(getActivity(), clz));

    }

}

完美的非常感谢,我很感激!完美的非常感谢,我很感激!