如何在android中按钮点击之间切换片段?

时间:2016-12-22 05:05:27

标签: android android-fragments

我正在创建一个Android应用。它包含两个活动。一个用于登录,另一个用于注册。它们都在不同的标签中,我可以通过滑动在它们之间切换。

但是如果需要登录,两个标签底部都有一个按钮。我需要在单击按钮时移动到注册选项卡,还有另一个按钮返回登录选项卡。

如何使用onclick侦听器事件实现它?

public static class PlaceholderFragment extends Fragment {

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }
    private static final String lTAG = RegisterActivity.class.getSimpleName();
    private Button btnLogin;
    private Button btnLinkToRegister;
    private EditText linputEmail;
    private EditText linputPassword;
    private ProgressDialog lpDialog;
    private SessionManager lsession;
    private SQLiteHandler ldb;
    private static final String rTAG = RegisterActivity.class.getSimpleName();
    private Button btnRegister;
    private Button btnLinkToLogin;
    private EditText rinputFullName;
    private EditText rinputEmail;
    private EditText rinputPassword;
    private ProgressDialog rpDialog;
    private SessionManager rsession;
    private SQLiteHandler rdb;
     public static Context context;

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

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

        if(getArguments().getInt(ARG_SECTION_NUMBER)==1) {

            View rootView = inflater.inflate(R.layout.activity_login, container, false);
            linputEmail = (EditText) rootView.findViewById(R.id.email);
            linputPassword = (EditText) rootView.findViewById(R.id.password);
            btnLogin = (Button) rootView.findViewById(R.id.btnLogin);
            btnLinkToRegister = (Button) rootView.findViewById(R.id.btnLinkToRegisterScreen);

            // Progress dialog
            lpDialog = new ProgressDialog(getActivity());
            lpDialog.setCancelable(false);

            // SQLite database handler
             context=getContext();
            ldb = new SQLiteHandler(context);

            // Session manager
            lsession = new SessionManager(context);

            // Check if user is already logged in or not
            if (lsession.isLoggedIn()) {
                // User is already logged in. Take him to main activity
                Intent intent = new Intent(context, MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }

            // Login button Click Event
            btnLogin.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    String email = linputEmail.getText().toString().trim();
                    String password = linputPassword.getText().toString().trim();


                    // Check for empty data in the form
                    if (!email.isEmpty() && !password.isEmpty()) {
                        // login user
                        checkLogin(email, password);
                    } else {
                        // Prompt user to enter credentials
                        Toast.makeText(context,
                                "Please enter the credentials!", Toast.LENGTH_LONG)
                                .show();
                    }
                }

            });

            // Link to Register Screen
            btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {

                    Intent i = new Intent(context,
                            RegisterActivity.class);
                    startActivity(i);
                    getActivity().finish();
                }
            });
            return  rootView;
        }

        else if (getArguments().getInt(ARG_SECTION_NUMBER)==2){

            View rootView = inflater.inflate(R.layout.activity_register, container, false);
            rinputFullName = (EditText) rootView.findViewById(R.id.name);
            rinputEmail = (EditText) rootView.findViewById(R.id.email);
            rinputPassword = (EditText) rootView.findViewById(R.id.password);
            btnRegister = (Button) rootView.findViewById(R.id.btnRegister);
            btnLinkToLogin = (Button) rootView.findViewById(R.id.btnLinkToLoginScreen);

            // Progress dialog
            rpDialog = new ProgressDialog(getActivity());
            rpDialog.setCancelable(false);

            // Session manager
            context=getContext();
            rsession = new SessionManager(context);

            // SQLite database handler
            rdb = new SQLiteHandler(context);

            // Check if user is already logged in or not
            if (rsession.isLoggedIn()) {
                // User is already logged in. Take him to main activity
                Intent intent = new Intent(context,
                        MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }

            // Register Button Click event
            btnRegister.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    String name = rinputFullName.getText().toString().trim();
                    String email = rinputEmail.getText().toString().trim();
                    String password = rinputPassword.getText().toString().trim();

            if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
                        if(isEmailValid(email)) {
                            registerUser(name, email, password);
                        }else{
                            Toast.makeText(context,
                                    "Please enter valid email!", Toast.LENGTH_LONG)
                                    .show();
                        }
                    } else {
                        Toast.makeText(context,
                                "Please enter your details!", Toast.LENGTH_LONG)
                                .show();
                    }
                }
            });

            // Link to Login Screen
            btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    Intent i = new Intent(context,
                            LoginActivity.class);
                    startActivity(i);
                    getActivity().finish();
                }
            });

            return  rootView;

        }
        return null;
    }
    ``
/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "LOGIN";
            case 1:
                return "REGISTER";

        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

这种情况有两种可能性: -

  1. 如果要将一个片段切换到另一个片段,则必须使用framelayout而不是viewpager。
  2. 如果您使用framelayout,则无法滑动标签。您必须单击标签才能对其进行更改。

    使用framelayout后,您可以从主活动中获取famelayout id,并且可以使用FragmentTransaction切换片段。

    按钮单击片段

         FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
         fragmentTransaction.replace(R.id.framelayout, new TwoFragment(), "NewFragment TAG");
         fragmentTransaction.addToBackStack(null);
         fragmentTransaction.commit();