대부분의 업무용 앱에서 필요한 로그인 화면과 메뉴 화면을 간단하게 만들고 두 화면 간을 전환하면서 토스트로 메시지를 띄워주도록 만들어보세요.
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:width="150sp" android:height="30dp"> <shape> <solid android:color="#ffffff"/> <stroke android:width="1.5dp" android:color="#aa33bbbb"/> </shape> </item> </layer-list>
<?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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:id="@+id/titleLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/mainColor"> <TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="로그인 하기" android:textSize="25sp" android:textColor="#ffffff" android:textStyle="bold"/> </LinearLayout> <LinearLayout android:id="@+id/txtLogin" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="100dp" android:gravity="center"> <Button android:id="@+id/btnLogin" android:layout_width="130dp" android:layout_height="wrap_content" android:layout_margin="20dp" android:backgroundTint="@color/mainTintColor" android:background="#aa33bbbb" android:textColor="#ffffff" android:textStyle="bold" android:text="로그인"/> </LinearLayout> </LinearLayout>
<?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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:id="@+id/titleLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/mainColor"> <TextView android:id="@+id/txtTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:text="메인 메뉴" android:textSize="25sp" android:textColor="#ffffff" android:textStyle="bold"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="250dp" android:gravity="center"> <Button android:id="@+id/btnCustomCare" android:layout_width="130dp" android:layout_height="wrap_content" android:background="@color/mainColor" android:layout_margin="20dp" android:text="고객 관리" android:textSize="20sp" android:textColor="#ffffff" android:textStyle="bold" android:backgroundTint="@color/mainTintColor"/> <Button android:id="@+id/btnSalesCare" android:layout_width="130dp" android:layout_height="wrap_content" android:background="@color/mainColor" android:layout_margin="20dp" android:text="매출 관리" android:textSize="20sp" android:textColor="#ffffff" android:textStyle="bold" android:backgroundTint="@color/mainTintColor"/> <Button android:id="@+id/btnProductCare" android:layout_width="130dp" android:layout_height="wrap_content" android:background="@color/mainColor" android:layout_margin="20dp" android:text="상품 관리" android:textSize="20sp" android:textColor="#ffffff" android:textStyle="bold" android:backgroundTint="@color/mainTintColor"/> </LinearLayout> </LinearLayout>
package com.study.doit; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContract; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Color; import android.nfc.Tag; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; public static final int MENU_CODE_LOGIN = 10001; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnLogin = findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { moveActivity(); } }); } ActivityResultLauncher startActivityResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { } }); public void moveActivity(){ Intent intent = new Intent(getApplicationContext(), MenuActivity.class); startActivityResult.launch(intent); } }
package com.study.doit; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.nfc.Tag; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MenuActivity extends AppCompatActivity { private static final String TAG = "MenuActivity"; public static final int MENU_CODE_CUSTOM = 10002; public static final int MENU_CODE_SALES = 10003; public static final int MENU_CODE_PRODUCT = 10004; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menu); Button btnCustomCare, btnProductCare, btnSalesCare; btnCustomCare = findViewById(R.id.btnCustomCare); btnProductCare = findViewById(R.id.btnProductCare); btnSalesCare = findViewById(R.id.btnSalesCare); btnCustomCare.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MenuActivity.this, MainActivity.class); Toast.makeText(getApplicationContext(), "고객 관리", Toast.LENGTH_SHORT).show(); backActivity.launch(intent); } }); btnSalesCare.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MenuActivity.this, MainActivity.class); Toast.makeText(getApplicationContext(), "매출 관리", Toast.LENGTH_SHORT).show(); backActivity.launch(intent); } }); btnProductCare.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MenuActivity.this, MainActivity.class); Toast.makeText(getApplicationContext(), "상품 관리", Toast.LENGTH_SHORT).show(); backActivity.launch(intent); } }); } ActivityResultLauncher backActivity = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { } }); }
좀 칙칙해서 UI를 꾸며보았습니다.
(작성자 성격이 꾸미면서 하는걸 좋아함.)
startActivityForResult
를 이용하려 하였는데 안드로이드 스튜디오 업데이트로 deprecated 되면서 ActivityResultLauncher
와registerForActivityResult
를 이용하여 intent
하는 방법을 배웠습니다.
기존 startActivity()
를 사용하려 하였는데
참고할 점에
각 화면은 액티비티로 만들고 화면 간 전환 시에는
startActivityForResult
메서드를 사용합니다.
라고 작성 돼 있어서 registerForActivityResult
를 사용하였습니다.
하나의 클릭 메서드로 버튼을 누르는 경우마다 해당 메뉴를 토스트해주는 방법이 있을 거 같은데 registerForActivityResult
를 잘 몰라서 각각의 버튼을 구현해주었습니다.
잘보고갑니다^오^