코린이 탈출기

chapter 4.2 - 실습해보기 본문

화면과 같이 만들어 보고 비교해보기 

아이디 "sohee@naver.com"와 비밀번호 "0000"입력 시 토스트에 "관리자 입니다" 띄우기

만일 둘 중 하나라도 다르다면 토스토에 "로그인 실패" 띄우기

예시

 

 

 

 

 

 

 

 

 

 

 

코드내용

 

 

 

 

 

 

 

 

 

 

xml코드

 

<androidx.constraintlayout.widget.ConstraintLayout 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:padding="10dp"
    tools:context=".Test">

    <ImageView
        android:id="@+id/loginImg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <TextView
        android:id="@+id/idTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="아이디"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toBottomOf="@id/loginImg"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <EditText
        android:id="@+id/idEdt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="이메일 형식으로 입력"
        android:inputType="textEmailAddress"
        app:layout_constraintTop_toBottomOf="@id/idTxt"/>

    <TextView
        android:id="@+id/pwTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="비밀번호"
        android:textSize="20sp"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toBottomOf="@id/idEdt"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <EditText
        android:id="@+id/pwEdt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="8자 이상"
        android:inputType="textPassword"
        app:layout_constraintTop_toBottomOf="@id/pwTxt"/>

    <Button
        android:id="@+id/loginBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="로그인"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

MainActivity 코드

 

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Btn.setOnClickListener {

val id = edtId.text.toString()
val password = edtPassword.text.toString()

if (id == "sohee@naver.com" && password == "0000") {
Toast.makeText(this, "관리자입니다.", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "로그인 실패", Toast.LENGTH_SHORT).show()
}
}
}
}
Comments