ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Fragment 예제
    Android 2020. 6. 3. 11:01

    1. mainActivity

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="20"
            android:text=""
            android:gravity="center_vertical"
            android:layout_marginStart="30dp"
            android:textSize="30dp"
            android:textColor="@color/colorBlack"
            />
    
        <!--일반로그인/간편로그인-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="10"
            android:orientation="horizontal"        >
    
            <TextView
                android:id="@+id/tv_login"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="50"
                android:text="일반로그인"
                android:gravity="center"
                android:textColor="@color/colorBlack"
                android:layout_marginStart="20dp"
                android:clickable="true"
                android:onClick="clickHandler">
    
    
            </TextView>
    
            <TextView
                android:id="@+id/tv_easy_login"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="50"
                android:text="간편로그인"
                android:gravity="center"
                android:textColor="@color/colorBlack"
                android:layout_marginEnd="20dp"
                android:clickable="true"
                android:onClick="clickHandler"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:orientation="horizontal">
            <View
                android:id="@+id/v_login"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="10"
                android:background="@color/colorBlack"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="5dp"/>
            <View
                android:id="@+id/v_easy_login"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="10"
                android:background="@color/colorBlack"
                android:layout_marginStart="5dp"
                android:layout_marginEnd="20dp"/>
    
        </LinearLayout>
    
        <FrameLayout
            android:id="@+id/fl_login"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="40"/>
    
    
    </LinearLayout>

    2. fragment A

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <!--아이디 비밀번호-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="20"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="10"
                android:text="아이디"
                android:layout_marginStart="20dp"
                android:textColor="@color/colorPrimary"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:hint="아이디를 입력하세요."
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="10"
                android:text="비밀번호"
                android:layout_marginStart="20dp"
                android:textColor="@color/colorPrimary"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:hint="비밀번호를 입력하세요."
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"/>
        </LinearLayout>
    
        <!--아이디찾기 / 비밀번호 찾기-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:orientation="horizontal"
            android:gravity="right"
            android:layout_marginEnd="20dp">
    
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="아이디 찾기"/>
    
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="/"/>
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="비밀번호 찾기"/>
    
        </LinearLayout>
    
        <!--로그인 회원가입 버튼-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="20"
            android:orientation="vertical">
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50"
                android:text="로그인"
                android:textColor="@color/colorWhite"
                android:background="@color/colorPrimary"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="10dp"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50"
                android:text="회원가입"
                android:background="@color/colorWhite"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"/>
    
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="10">
    
        </LinearLayout>
    
    </LinearLayout>
    package com.example.packagename.fragment;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    import com.example.packagename.R;
    
    public class LoginFragment extends Fragment {
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_login, container, false);
        }
    }
    

    3. fragment B

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <!--간편 비밀번호 6자리-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="20"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="30"
                android:text="간편비밀번호 6자리를 입력해주세요."
                android:gravity="center"
                android:textColor="@color/colorBlack"/>
            <EditText
                android:text="1234"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="10"
                android:layout_marginStart="30dp"
                android:layout_marginEnd="30dp"/>
        </LinearLayout>
    
        <!--로그인 회원가입 버튼-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="20"
            android:orientation="vertical">
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50"
                android:text="로그인"
                android:textColor="@color/colorWhite"
                android:background="@color/colorPrimary"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="10dp"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50"
                android:text="회원가입"
                android:background="@color/colorWhite"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"/>
    
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="10">
    
        </LinearLayout>
    
    </LinearLayout>
    package com.example.packagename.fragment;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    import com.example.packagename.R;
    
    public class EasyLoginFragment extends Fragment {
    
    
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_easy_login, container, false);
        }
    }
    

    4. mainactivity에 fragment 연결

    package com.example.packagename;
    
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    
    import com.example.packagename.fragment.EasyLoginFragment;
    import com.example.packagename.fragment.LoginFragment;
    
    public class MainActivity extends AppCompatActivity {
        private FragmentManager fragmentManager;
        private LoginFragment loginFragment;
        private EasyLoginFragment easyLoginFragment;
        private FragmentTransaction transaction;
        private TextView tvLogin, tvEasyLogin;
        private View vLogin, vEasyLogin;
        private String colorPrimary = "#6200EE", colorBlack = "#000000";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            init();
    
    
            fragmentManager = getSupportFragmentManager();
    
            loginFragment = new LoginFragment();
            easyLoginFragment = new EasyLoginFragment();
    
            transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fl_login, loginFragment).commitAllowingStateLoss();
    
    
        }
        public void init(){
            tvLogin = findViewById(R.id.tv_login);
            tvEasyLogin = findViewById(R.id.tv_easy_login);
            vLogin = findViewById(R.id.v_login);
            vEasyLogin = findViewById(R.id.v_easy_login);
            tvLogin.setTextColor(Color.parseColor(colorPrimary));
            vLogin.setBackgroundColor(Color.parseColor(colorPrimary));
        }
    
    
        public void clickHandler(View view){
    
            transaction = fragmentManager.beginTransaction();
    
            switch (view.getId()){
                case R.id.tv_login:
                    tvLogin.setTextColor(Color.parseColor(colorPrimary));
                    vLogin.setBackgroundColor(Color.parseColor(colorPrimary));
                    tvEasyLogin.setTextColor(Color.parseColor(colorBlack));
                    vEasyLogin.setBackgroundColor(Color.parseColor(colorBlack));
                    transaction.replace(R.id.fl_login, loginFragment).commitNowAllowingStateLoss();
                    break;
                case R.id.tv_easy_login:
                    tvEasyLogin.setTextColor(Color.parseColor(colorPrimary));
                    vEasyLogin.setBackgroundColor(Color.parseColor(colorPrimary));
                    tvLogin.setTextColor(Color.parseColor(colorBlack));
                    vLogin.setBackgroundColor(Color.parseColor(colorBlack));
                    transaction.replace(R.id.fl_login, easyLoginFragment).commitNowAllowingStateLoss();
                    break;
            }
        }
    
    
    }

Designed by Tistory.