如何让活动打开片段?

时间:2015-03-24 12:49:55

标签: android android-intent android-fragments android-activity

我有一个名为Chap.java的片段和一个名为Exercise.java的活动。 我设法获取片段Chap.java来打开Activity.java活动。 但是,对我来说,这是另一种方式吗? 我的意思是让活动打开片段。

这是我目前的代码:

我的Chap.java

package com.boszcorp.newbuttontestcrezi;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import static com.boszcorp.newbuttontestcrezi.R.layout.chapter;

/**
 * Created by Aizad on 23/03/2015.
 */
public class Chap extends Fragment {
    View rootview;

    @Nullable

    //@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(chapter, container, false);
        Button newPage = (Button)rootview.findViewById(R.id.buttonEx);
        newPage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
        Intent intent = new Intent(getActivity(), Exercise.class);
        startActivity(intent);




    }
});
        return rootview;};
}

我的Exercise.java

package com.boszcorp.newbuttontestcrezi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by Aizad on 23/03/2015.
 */
public class Exercise extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exercise);
        Button newReturn = (Button)findViewById(R.id.buttonChap);
        newReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent (Exercise.this, Chap.class);
                startActivity(intent);
            }
        });


    }
}

3 个答案:

答案 0 :(得分:0)

在onClick()中使用此代码:

Chap mChap = new Chap();
FragmentManager fragmentManager = getFragmentManager() or getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, mChap);
fragmentTransaction.commit();

希望这会有效..

答案 1 :(得分:0)

是的,你可以。试试这个:

Chap fragment = new Chap();
FragmentManager fm = getSupportFragmentManager(); //or getFragmentManager() if you are not using support library.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.your_container, fragment);
ft.commit();

希望这有帮助。

答案 2 :(得分:0)

您可以这样做,创建一个扩展活动的新类,并在新类中执行以下操作:

将新类的内容视图设置为片段的布局,并添加以下代码:

Chap f1 = new Chap();       
getSupportFragmentManager().beginTransaction().add(R.id.fragment_detail, f1).commit();

希望这会对你有所帮助。

相关问题