전체 코드 : https://github.com/ryr0121/AndroidPractice/tree/main/sosInfoApp
구현 결과 :

주요 기능
- 의료정보(이름, 생년월일, 혈액형, 비상 연락처) 조회/입력/수정/삭제
- 비상 연락처 탭하면 전화 앱으로 연결
ConstraintLayout으로 view 배치
RadioButton, Spinner를 이용한 범위 내 값 선택 구현
// Rh+, Rh- 중 하나를 선택하는 RadioButton 그룹
<RadioGroup
android:id="@+id/bloodTypeRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@id/bloodTypeTextView"
app:layout_constraintEnd_toStartOf="@id/bloodTypeSpinner"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintTop_toTopOf="@id/bloodTypeTextView">
<RadioButton
android:id="@+id/bloodTypePlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rh+" />
<RadioButton
android:id="@+id/bloodTypeMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rh-" />
</RadioGroup>
// A, B, AB, O형 중 하나를 선택하는 Spinner
<Spinner
android:id="@+id/bloodTypeSpinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@id/bloodTypeTextView"
app:layout_constraintEnd_toEndOf="@+id/nameEditText"
app:layout_constraintTop_toTopOf="@id/bloodTypeTextView" />
Intent를 이용한 화면 전환 및 전화앱 연결 구현
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.moveToInputActivity.setOnClickListener {
val intent = Intent(this, EditActivity::class.java)
startActivity(intent)
}
...
binding.moveToSosCallLayer.setOnClickListener {
with(Intent(Intent.ACTION_VIEW)) {
val phoneNum = binding.sosPhoneValueTextView.text.toString().replace("-","")
data = Uri.parse("tel:$phoneNum")
startActivity(this)
}
}
}
SharedPreferences를 이용해 의료정보 데이터 관리
- 데이터 조회
with(getSharedPreferences(USER_INFO, Context.MODE_PRIVATE)) {
binding.nameValueTextView.text = getString(NAME, "미입력")
binding.birthValueTextView.text = getString(BIRTH, "미입력")
binding.bloodTypeValueTextView.text = getString(BLOOD_TYPE, "미입력")
binding.sosPhoneValueTextView.text = getString(SOS_PHONE, "미입력")
val notiInfo = getString(NOTI_INFO, "")
binding.notiInfoTextView.isVisible = notiInfo.isNullOrEmpty().not()
binding.notiInfoValueTextView.isVisible = notiInfo.isNullOrEmpty().not()
if(!notiInfo.isNullOrEmpty()) {
binding.notiInfoValueTextView.text = notiInfo
}
}
- 데이터 수정 및 저장
with(getSharedPreferences(USER_INFO, Context.MODE_PRIVATE).edit()) {
putString(NAME, binding.nameEditText.text.toString())
putString(BLOOD_TYPE, getBloodType())
putString(SOS_PHONE, binding.sosPhoneValueTextView.text.toString())
putString(BIRTH, binding.birthEditText.text.toString())
putString(NOTI_INFO, getNotiInfo())
apply()
}
- 데이터 삭제
with(getSharedPreferences(USER_INFO, Context.MODE_PRIVATE).edit()) {
clear()
apply()
getDataAndUiUpdate() // 데이터 재조회 및 UI 업데이트
}
'Android' 카테고리의 다른 글
[Android] "스톱워치 앱" 구현 (0) | 2024.07.02 |
---|---|
[Android] "계산기 앱" 구현 (0) | 2024.06.30 |
[Android] "단위 변환기 앱" 구현 (4) | 2024.06.26 |
[Android] "숫자세기 앱" 구현 (7) | 2024.06.25 |