如何将登录屏幕放置在片段内?

问题描述 投票:0回答:1

我正在尝试构建一个 Android 应用程序,其中有一个导航抽屉,其中有 5 个不同的选项。 对于第一个选项,我正在尝试实现登录屏幕。但是,我无法让应用程序切换到注册屏幕,反之亦然。

这是我的代码:

这是登录活动:

public class LoginActivity extends MainActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setting default screen to login.xml
        setContentView(R.layout.login);

        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });
    }
}

这是注册活动:

public class RegisterActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.register);

        TextView loginScreen = (TextView) findViewById(R.id.link_to_login);

        // Listening to Login Screen link
        loginScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Closing registration screen
                // Switching to Login Screen/closing register screen
                finish();
            }
        });
    }
}

这是登录片段(我想在其中实现上述 2 个活动):

import android.app.Activity;
import android.os.Bundle;
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;
import android.widget.TextView;

import com.a.myapplication.R;


public class LoginFragment extends Fragment {

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

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



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


        // Inflate the layout for this fragment


        return rootView;
    }

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

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

这是register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:layout_below="@id/nav_header_container">
    <!-- Full Name Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="Full Name"/>
    <EditText android:id="@+id/reg_fullname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:layout_marginBottom="20dip"/>
    <!--  Email Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="Email"/>
    <EditText android:id="@+id/reg_email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:layout_marginBottom="20dip"/>
    <!-- Password Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="Password"/>
    <EditText android:id="@+id/reg_password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:password="true"
        android:singleLine="true"
        android:layout_marginTop="5dip"/>
    <!-- Register Button -->
    <Button android:id="@+id/btnRegister"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Register New Account"/>
    <!-- Link to Login Screen -->
    <TextView android:id="@+id/link_to_login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:layout_marginBottom="40dip"
        android:text="Already has account! Login here"
        android:gravity="center"
        android:textSize="20dip"
        android:textColor="#025f7c"/>
</LinearLayout>

这是登录.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:layout_below="@id/toolbar">
    <!--  Email Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="Email"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="20dip"
        android:singleLine="true"/>
    <!--  Password Label -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#372c24"
        android:text="Password"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:password="true"/>
    <!-- Login button -->
    <Button android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Login"/>
    <!-- Link to Registration Screen -->
    <TextView android:id="@+id/link_to_register"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:layout_marginBottom="40dip"
        android:text="Register here"
        android:gravity="center"
        android:textSize="20dip"
        android:textColor="#0b84aa"/>

</LinearLayout>
    <!-- Login Form Ends -->

这是我的 MainActivity.java:

    public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {

    private static String TAG = MainActivity.class.getSimpleName();

    private Toolbar mToolbar;
    private FragmentDrawer drawerFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        drawerFragment = (FragmentDrawer)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout),
                mToolbar);
        drawerFragment.setDrawerListener(this);


        // display the first navigation drawer view on app launch
        displayView(0);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if(id == R.id.action_search){
            Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDrawerItemSelected(View view, int position) {
        displayView(position);
    }

    private void displayView(int position) {
        Fragment fragment = null;
        String title = getString(R.string.app_name);
        switch (position) {
            case 0:
                fragment = new LoginFragment();
/*
I am trying to call this fragment, that includes the two activities
*/
                title = getString(R.string.login);
                break;
            case 1:
                fragment = new CalendarFragment();
                title = getString(R.string.title_calendar);
                break;
            case 2:
                fragment = new GalleryFragment();
                title = getString(R.string.title_gallery);
                break;
            case 3:
                fragment = new BankFragment();
                title = getString(R.string.title_bank);
                break;
            case 4:
                fragment = new SettingsFragment();
                title = getString(R.string.title_settings);
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.commit();

            // set the toolbar title
            getSupportActionBar().setTitle(title);
        }
    }
}

还有一个额外的fragmentdrawer用于在屏幕上绘制片段。 非常感谢任何帮助。

我尝试添加 onClick() 事件来处理切换,但这对我来说并没有真正起作用。

android android-fragments android-intent android-activity
1个回答
0
投票

我也想这样做,但经过我的研究,我决定最好直接在片段中使用我们的布局,而不是在片段中调用活动对象并在特定片段中加载活动代码。

编辑您的 LoginFragment :

public class LoginFragment extends Fragment {

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

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



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


        // Inflate the layout for this fragment

TextView registerScreen = (TextView) rootView.findViewById(R.id.link_to_register);

        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });

        return rootView;
    }

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

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.