fragment_info.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:orientation="vertical" tools:context=".InfoFragment"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name:" android:textSize="18sp" /> <EditText android:id="@+id/name" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:background="#def" android:padding="5dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Address:" android:textSize="18sp" /> <EditText android:id="@+id/address" android:layout_width="300dp" android:layout_height="wrap_content" android:background="#def" android:gravity="top|left" android:lines="3" android:padding="5dp" /> <Button android:id="@+id/buttonInfo_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:text="OK" /> </LinearLayout> |
fragment_login.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:columnCount="2" android:orientation="horizontal" tools:context=".LoginFragment"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Email:" android:textSize="18sp" /> <EditText android:id="@+id/email" android:layout_width="250dp" android:layout_height="wrap_content" android:background="#def" android:padding="5dp" /> <Space android:layout_height="20dp" android:layout_columnSpan="2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:text="Password:" android:textSize="18sp" /> <EditText android:id="@+id/password" android:layout_width="250dp" android:layout_height="wrap_content" android:background="#def" android:inputType="textPassword" android:padding="5dp" /> <Button android:id="@+id/buttonLogin_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_columnSpan="2" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:text="OK" /> </GridLayout> |
InfoFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * A simple {@link Fragment} subclass. */ public class InfoFragment extends Fragment { public InfoFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_info, container, false); } @Override public void onViewCreated(View view, Bundle bundle) { final EditText name = view.findViewById(R.id.name); final EditText address = view.findViewById(R.id.address); Button btOK = view.findViewById(R.id.buttonInfo_ok); btOK.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String msg = "ขอบคุณค่ะ"; String n = name.getText().toString().trim(); String a = address.getText().toString().trim(); if (n.length() == 0 || a.length() == 0) { msg = "กรุณากำหนดข้อมูลให้ครบด้วยค่ะ"; } Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show(); } }); } } |
LoginFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * A simple {@link Fragment} subclass. */ public class LoginFragment extends Fragment { public LoginFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_login, container, false); } @Override public void onViewCreated(View view, Bundle bundle) { final EditText email = view.findViewById(R.id.email); final EditText password = view.findViewById(R.id.password); Button btOK = view.findViewById(R.id.buttonLogin_ok); btOK.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String msg = "ขอบคุณค่ะ"; String n = email.getText().toString().trim(); String a = password.getText().toString().trim(); if (n.length() == 0 || a.length() == 0) { msg = "กรุณากำหนดข้อมูลให้ครบด้วยค่ะ"; } Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show(); } }); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <GridLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="20dp"> <Button android:id="@+id/buttonLogin" android:text="Login" android:textSize="16sp" /> <Button android:id="@+id/buttonInfo" android:text="Personal Info" android:textSize="16sp" /> </GridLayout> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.phaisarn.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.support.v4.app.Fragment; public class MainActivity extends AppCompatActivity { private LoginFragment loginFragment; private InfoFragment infoFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button buttonLogin = findViewById(R.id.buttonLogin); buttonLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (loginFragment == null) { loginFragment = new LoginFragment(); } showFragment(loginFragment); } }); Button buttonInfo = findViewById(R.id.buttonInfo); buttonInfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (infoFragment == null) { infoFragment = new InfoFragment(); } showFragment(infoFragment); } }); } private void showFragment(Fragment fragment) { getSupportFragmentManager() .beginTransaction() .replace(R.id.frameLayout, fragment) .addToBackStack(null) .commit(); } } |