안드로이드 스튜디오를 활용해 자기소개 어플을 만들어보려고 한다. 기능에는 회원가입, 로그인, 랜덤 이미지 출력, 아이디와 비밀번호 유지기능을 구현해 보도록 하겠다
package com.example.login
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
class SignInActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val input_id = findViewById<EditText>(R.id.et_id)
val input_pw = findViewById<EditText>(R.id.et_pw)
val btn_Signup = findViewById<Button>(R.id.btn_signup)
val btn_login = findViewById<Button>(R.id.btn_login)
val strid = intent.getStringExtra("id").toString()
val strpw = intent.getStringExtra("password").toString()
val strname = intent.getStringExtra("name").toString()
setResultSignUp()
// 회원가입 버튼
btn_Signup.setOnClickListener {
val intent = Intent(this, SignupActivity::class.java)
startActivity(intent)
}
// 로그인버튼
btn_login.setOnClickListener {
val ip_id = input_id.text.toString()
val ip_pw = input_pw.text.toString()
if (ip_id.isEmpty() || ip_pw.isEmpty()) {
Toast.makeText(this, "입력되지 않은 정보가 있습니다.", Toast.LENGTH_SHORT).show()
}
else if((ip_id == strid) && (ip_pw == strpw)){
Toast.makeText(this, "로그인 성공!", Toast.LENGTH_SHORT).show()
val intent = Intent(this, HomeActivity::class.java)
intent.putExtra("id",strid)
intent.putExtra("name",strname)
startActivity(intent)
}
else{
Toast.makeText(this, "아이디와 비밀번호를 확인해주세요.", Toast.LENGTH_SHORT).show()
}
}
}
//종료 버튼을 누르면 데이터 id, password 출력
private lateinit var resultLauncher: ActivityResultLauncher<Intent>
private fun setResultSignUp() {
resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
val id = data?.getStringExtra("id")
val password = data?.getStringExtra("password")
val getId = findViewById<EditText>(R.id.et_id)
getId.setText(id)
val getPw = findViewById<EditText>(R.id.et_pw)
getPw.setText(password)
}
}
}
}
package com.example.login
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
class SignupActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_signp)
val et_name = findViewById<EditText>(R.id.et_name)
val et_id = findViewById<EditText>(R.id.et_signid)
val et_pw = findViewById<EditText>(R.id.et_signpw)
val btn_sign = findViewById<Button>(R.id.signup)
//회원가입 기능
btn_sign.setOnClickListener {
val name = et_name.text.toString()
val id = et_id.text.toString()
val pw = et_pw.text.toString()
if (name.isEmpty() || id.isEmpty() || pw.isEmpty()) {
Toast.makeText(this, "입력되지 않은 정보가 있습니다.", Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(this, "회원가입 성공!", Toast.LENGTH_SHORT).show()
val intent = Intent(this, SignInActivity::class.java)
intent.putExtra("id",id)
intent.putExtra("password", pw)
intent.putExtra("name",name)
startActivity(intent)
}
}
}
}
package com.example.login
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import kotlin.random.Random
class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val myid = intent.getStringExtra("id")
val myname = intent.getStringExtra("name")
val idText = findViewById<TextView>(R.id.idtext)
val nameText = findViewById<TextView>(R.id.nametext)
idText.text = " ◼ ID : $myid "
nameText.text= " ◼ 이름 : $myname "
//랜덤으로 이미지 출력
val photoImage = findViewById<ImageView>(R.id.imageView)
when(Random.nextInt(1, 6)) {
1 -> photoImage.setImageResource(R.drawable.character1)
2 -> photoImage.setImageResource(R.drawable.character2)
3 -> photoImage.setImageResource(R.drawable.character3)
4 -> photoImage.setImageResource(R.drawable.character4)
5 -> photoImage.setImageResource(R.drawable.character5)
}
//종료
val btn_finish = findViewById<Button>(R.id.btn_end)
btn_finish.setOnClickListener{
finish()
}
}
}
오늘 TIL은 액티비티만 간단하게 설명했는데 다음에는 세세하게 기능을 설명하도록 하겠다.