<LinearLayout ...?
...
<EditText ... />
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.text_views) // 이 부분이 변경됨
}
fun doAction(v:View) {
// Shows a Toast message in response to button
Toast.makeText(getApplicationContext(), "Submitted Successfully"
Toast.LENGTH_SHORT).show();
}
}
버튼 위젯을 정의한 xml 레이아웃 파일(예, text_views.xml)에서,
버튼 위젯의 onClick 속성에 앞 단계에서 추가한 메소드(예, doAction())를 설정한다.
<LinearLayout ...>
...
이 방법에서는 이벤트를 처리하는 객체를 생성하여 해당 이벤트를 발생시키는 위젯에 등록한다.
위젯에서는 이벤트가 발생하면 등록된 이벤트 처리 객체가 정의된 일을 수행한다.
절차
<LinearLayout ...>
...
이벤트 처리 객체 생성 및 등록
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// text_views.xml에 정의된 View 객체 중에서 id가 submit_button인 것을 찾아 반환함
val btn = findViewById<Button?(R.id.submit_button)
//버튼이 클릭되었을 때 발생되는 클릭 이벤트를 처리하기 위해서는 View.OnClickListener 인터페이스를 구현
btn.setOnClickListener{
Toast.makeText(this, "Submitted Successfully",Toast.LENGTH_SHORT).show()
}
}
// fun doAction(v: View) {
// Toast.makeText(this, "Submitted Successfully",Toast.LENGTH_SHORT).show()
// }
}
// lv1. 로그인 페이지 만들기
package com.example.login
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class SignInActivity : AppCompatActivity() {
private lateinit var editTextId: EditText
private lateinit var editTextPassword: EditText
private lateinit var buttonLogin: Button
private lateinit var buttonSignUp: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sign_in)
editTextId = findViewById(R.id.editTextId)
editTextPassword = findViewById(R.id.editTextPassword)
buttonLogin = findViewById(R.id.buttonLogin)
buttonSignUp = findViewById(R.id.buttonSignUp)
buttonLogin.setOnClickListener {
val id = editTextId.text.toString()
val password = editTextPassword.text.toString()
if (id.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "아이디/비밀번호를 확인해주세요", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "로그인 성공", Toast.LENGTH_SHORT).show()
// Get the user information from SharedPreferences
val sharedPrefs = getSharedPreferences("MyPrefs", MODE_PRIVATE)
val name = sharedPrefs.getString("userName", "이름 없음")
val age = sharedPrefs.getString("userAge", "나이 없음")
val mbti = sharedPrefs.getString("userMBTI", "MBTI 없음")
val intent = Intent(this, HomeActivity::class.java)
intent.putExtra("userId", id)
intent.putExtra("userName", name)
intent.putExtra("userAge", age)
intent.putExtra("userMBTI", mbti)
startActivity(intent)
}
}
buttonSignUp.setOnClickListener {
startActivity(Intent(this, SignUpActivity::class.java))
}
}
}
// lv2. 회원가입 페이지 만들기
package com.example.login
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class SignUpActivity : AppCompatActivity() {
private lateinit var editTextName: EditText
private lateinit var editTextId: EditText
private lateinit var editTextPassword: EditText
private lateinit var editTextAge: EditText
private lateinit var editTextMBTI: EditText
private lateinit var buttonSignUp: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sign_up)
editTextName = findViewById(R.id.editTextName)
editTextId = findViewById(R.id.editTextId)
editTextPassword = findViewById(R.id.editTextPassword)
editTextAge = findViewById(R.id.editTextAge)
editTextMBTI = findViewById(R.id.editTextMBTI)
buttonSignUp = findViewById(R.id.buttonSignUp)
buttonSignUp.setOnClickListener {
val name = editTextName.text.toString()
val id = editTextId.text.toString()
val password = editTextPassword.text.toString()
val age = editTextAge.text.toString()
val mbti = editTextMBTI.text.toString()
if (name.isEmpty() || id.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "입력되지 않은 정보가 있습니다.", Toast.LENGTH_SHORT).show()
} else {
// Store the user information in SharedPreferences
val sharedPrefs = getSharedPreferences("MyPrefs", MODE_PRIVATE)
val editor = sharedPrefs.edit()
editor.putString("userId", id)
editor.putString("userName", name)
editor.putString("userAge", age)
editor.putString("userMBTI", mbti)
editor.apply()
// Start the HomeActivity
val intent = Intent(this, HomeActivity::class.java)
intent.putExtra("userId", id)
intent.putExtra("userName", name)
intent.putExtra("userAge", age)
intent.putExtra("userMBTI", mbti)
startActivity(intent)
finish()
}
}
}
}
// lv3. 자기소개 페이지 만들기
package com.example.login
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class HomeActivity : AppCompatActivity() {
private lateinit var textViewUserId: TextView
private lateinit var textViewName: TextView
private lateinit var textViewAge: TextView
private lateinit var textViewMBTI: TextView
private lateinit var buttonLogout: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
textViewUserId = findViewById(R.id.textViewUserId)
textViewName = findViewById(R.id.textViewName)
textViewAge = findViewById(R.id.textViewAge)
textViewMBTI = findViewById(R.id.textViewMBTI)
buttonLogout = findViewById(R.id.buttonLogout)
val intent = intent
val userId = intent.getStringExtra("userId")
val userName = intent.getStringExtra("userName") // Receive the name from the intent
val userAge = intent.getStringExtra("userAge") // Receive the age from the intent
val userMBTI = intent.getStringExtra("userMBTI") // Receive the MBTI from the intent
textViewUserId.text = "아이디: $userId"
textViewName.text = "이름: $userName"
textViewAge.text = "나이: $userAge"
textViewMBTI.text = "MBTI: $userMBTI"
buttonLogout.setOnClickListener {
finish()
}
}
}
<ImageView
android:id="@+id/imageViewLogo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:src="@drawable/logo_image" /> <!-- 여기서 'logo_image'는 로고 이미지의 리소스 이름입니다. -->
<EditText
android:id="@+id/editTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="아이디를 입력하세요."
android:inputType="text"
android:padding="32dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="비밀번호를 입력하세요."
android:inputType="textVisiblePassword" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="로그인"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/buttonSignUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="회원가입"
android:layout_marginTop="16dp" />
<ImageView
android:id="@+id/imageViewTitle"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:src="@drawable/title_image" />
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageViewTitle"
android:hint="이름을 입력하세요."
android:inputType="text"
android:padding="16dp" />
<EditText
android:id="@+id/editTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextName"
android:hint="아이디를 입력하세요."
android:inputType="text"
android:padding="16dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextId"
android:hint="비밀번호를 입력하세요."
android:inputType="textPassword"
android:padding="16dp" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextPassword"
android:hint="나이를 입력하세요."
android:inputType="text"
android:padding="16dp" />
<EditText
android:id="@+id/editTextMBTI"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextAge"
android:hint="MBTI를 입력하세요."
android:inputType="text"
android:padding="16dp" />
<Button
android:id="@+id/buttonSignUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="회원가입"
android:layout_below="@id/editTextPassword"
android:layout_marginTop="120dp" />
<ImageView
android:id="@+id/imageViewIntroduce"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:src="@drawable/introduce_image" /> <!-- Add margin bottom to create spacing -->
<TextView
android:id="@+id/textViewUserId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="아이디: "
android:textSize="20sp"
android:layout_below="@id/imageViewIntroduce"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" /> <!-- Add margin top to create spacing -->
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름: "
android:textSize="20sp"
android:layout_below="@id/textViewUserId"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp" /> <!-- Add margin top to create spacing -->
<TextView
android:id="@+id/textViewAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="나이: "
android:textSize="20sp"
android:layout_below="@id/textViewName"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp" /> <!-- Add margin top to create spacing -->
<TextView
android:id="@+id/textViewMBTI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MBTI: "
android:textSize="20sp"
android:layout_below="@id/textViewAge"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp" /> <!-- Add margin top to create spacing -->
<Button
android:id="@+id/buttonLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="종료"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="64dp"/>