如何在Fragments中再添加一个按钮

时间:2015-09-17 10:33:32

标签: android button android-fragments android-activity

我已经给出了我解决的代码并且它工作正常但我不知道如何再添加一个按钮来重定向到另一个活动

public class courses extends Fragment  {

    Intent intent;


    @Nullable
    @Override


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.courses, container, false);
        intent = new Intent(getActivity(), software_course.class);
        final Button button = (Button) root.findViewById(R.id.Software_Course);



        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        });

        return root; 
     } }

2 个答案:

答案 0 :(得分:3)

在真正答案之前只有一些提示:

  • 在课程中使用第一个大写字母'名称
  • 不要使用第一个大写字母表示ids
  • 不要创建不同的新OnClickListener实例,如下所示,而是实现接口
  • 全局意图变量没有多大意义

现在这里是快速的'回答:

public class Courses extends Fragment {
    Intent intent, anotherIntent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.courses, container, false);
        intent = new Intent(getActivity(), Software_course.class);
        anotherIntent = new Intent(getActivity(), YourSecondActivityName.class);
        final Button button = (Button) root.findViewById(R.id.software_Course);
        final Button button2 = (Button) root.findViewById(R.id.software_Course2);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(intent);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(anotherIntent);
            }
        });

        return root; 
    }
}

以下是我的建议:

public class Courses extends Fragment implements View.OnClickListener {    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.courses, container, false);
        root.findViewById(R.id.software_Course).setOnClickListener(this);
        root.findViewById(R.id.software_Course2).setOnClickListener(this);         

        return root; 
    }

    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.software_Course:
                startActivity(new Intent(getActivity(), Software_course.class));
                break;
            case R.id.software_Course2:
                startActivity(new Intent(getActivity(), YourSecondActivityName.class));
                break;
        }
    }
}

答案 1 :(得分:1)

courses.xml添加另一个按钮,其中包含其他ID的其他按钮。

<Button
        android:id="@+id/hardware"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

courses.java初始化按钮中,使用setonClickListener

public class courses extends Fragment  {

    Intent intent;


    @Nullable
    @Override


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

          View root = inflater.inflate(R.layout.courses, container, false);
    intent = new Intent(getActivity(), software_course.class);
    final Button button = (Button) root.findViewById(R.id.Software_Course);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(intent);
        }
    });

    Button Hardware=(Button)root.findViewById(R.id.Hardware_Course);
    Hardware.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Do stuff you want to do on click of button
            intent = new Intent(getActivity(), hardware_course.class);
            startActivity(intent);
        }
    });

return root;
}

}