如何使用按钮在片段之间切换

时间:2016-07-24 16:04:07

标签: java android

片段1:

public class homePage extends Fragment {


private OnFragmentInteractionListener mListener;
private View view;

public homePage() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_home_page, container, false);
    Button btnLogin = (Button) view.findViewById(R.id.login);
    Button btnRegister = (Button) view.findViewById(R.id.register);
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            loginView();
        }
    });
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            registerView();
        }
    });
    return view;
}


public static homePage newInstance() {
    homePage fragment = new homePage();
    Bundle args = new Bundle();
    return fragment;
}


public void registerView(){}
public void loginView(){}


public interface OnFragmentInteractionListener {

}
}

Acitivty:

 public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        homePage homepage = new homePage();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, homepage)
                    .commit();
        }
    }




 }

我的fragment1有两个按钮,其id为“login”和“register”。每当点击登录时,我想转到我的fragment_login.xml,当点击寄存器时,我想转到我的fragment_register.xml。我应该在我的活动或片段中创建这些功能吗?我该怎么做?我对android很新,我正在尝试学习这些基本的东西。谢谢你的帮助:(

登录上下文:“com.example.hoofdgebruiker.winkelskortrijk.login” 注册上下文:“com.example.hoofdgebruiker.winkelskortrijk.register”

1 个答案:

答案 0 :(得分:1)

您必须通过FragmentInterface个对象之间进行通信。

在片段之间切换的活动需要实现Interface

因此,在您的情况下,您的类必须定义如下。这是您的Interface,它可以帮助您告诉您的活动在Fragment内点击了一个按钮:

public interface HomeClickListener {
    void onLoginClick();
    void onRegisterClick();
}

您的活动需要实现上述界面,以便在单击按钮时收到通知:

public class HomeActivity extends Activity implements HomeClickListener {

    void onLoginClick() {
        // Your activity code to replace your current Fragment with your Login fragment
    }

    void onRegisterClick() {
        // Your activity code to replace your current Fragment with your Register fragment
    }

}

您的片段由一个活动托管,因此您需要引用它并通过Interface通知它:

public class LoginFragment extends Fragment {

    final HomeClickListener homeClickListener

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            homeClickListener = (homeClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_page, container, false);
        Button btnLogin = (Button) view.findViewById(R.id.login);
        Button btnRegister = (Button) view.findViewById(R.id.register);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                homeClickListener.onLoginClick();
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                homeClickListener.onRegisterClick();
            }
        });
        return view;
    }

}

更多信息here