Урок 18. Практика (Код урока)

MainActivity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.view.isVisible
import com.neco_desarrollo.myapplication.constance.Constance
import com.neco_desarrollo.myapplication.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
lateinit var bindingClass: ActivityMainBinding
private var login: String = "empty"
private var password: String = "empty"
private var name: String = "empty"
private var name2: String = "empty"
private var name3: String = "empty"
private var avatarImageId: Int = 0

override fun onCreate(s: Bundle?) {
super.onCreate(s)
bindingClass = ActivityMainBinding.inflate(layoutInflater)
setContentView(bindingClass.root)

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if(requestCode == Constance.REQUEST_CODE_SIGN_IN){
val l = data?.getStringExtra(Constance.LOGIN)
val p = data?.getStringExtra(Constance.PASSWORD)
if(login == l && password == p){

bindingClass.imAvatar.visibility = View.VISIBLE
bindingClass.imAvatar.setImageResource(avatarImageId)
val textInfo = "$name $name2 $name3"
bindingClass.tvInfo.text = textInfo
bindingClass.bHide.visibility = View.GONE
bindingClass.bExit.text = "Выйти"

} else {

bindingClass.imAvatar.visibility = View.VISIBLE
bindingClass.imAvatar.setImageResource(R.drawable.dula)
bindingClass.tvInfo.text = "Такого аккаунта не существует!"

}

} else if(requestCode == Constance.REQUEST_CODE_SIGN_UP){

login = data?.getStringExtra(Constance.LOGIN)!!
password = data.getStringExtra(Constance.PASSWORD)!!
name = data.getStringExtra(Constance.NAME)!!
name2 = data.getStringExtra(Constance.NAME2)!!
name3 = data.getStringExtra(Constance.NAME3)!!
avatarImageId = data.getIntExtra(Constance.AVATAR_ID, 0)
bindingClass.imAvatar.visibility = View.VISIBLE
bindingClass.imAvatar.setImageResource(avatarImageId)
val textInfo = "$name $name2 $name3"
bindingClass.tvInfo.text = textInfo
bindingClass.bHide.visibility = View.GONE
bindingClass.bExit.text = "Выйти"

}
}

fun onClickSignIn(view: View){

if(bindingClass.imAvatar.isVisible && bindingClass.tvInfo.text.toString() != "Такого аккаунта не существует!"){

bindingClass.imAvatar.visibility = View.INVISIBLE
bindingClass.tvInfo.text = ""
bindingClass.bHide.visibility = View.VISIBLE
bindingClass.bExit.text = getString(R.string.sign_in)

} else {

val intent = Intent(this, SignInUpAct::class.java)
intent.putExtra(Constance.SIGN_STATE, Constance.SIGN_IN_STATE)
startActivityForResult(intent, Constance.REQUEST_CODE_SIGN_IN)

}

}

fun onClickSignUp(view: View){

val intent = Intent(this, SignInUpAct::class.java)
intent.putExtra(Constance.SIGN_STATE, Constance.SIGN_UP_STATE)
startActivityForResult(intent, Constance.REQUEST_CODE_SIGN_UP)

}


}

SignInUpAct

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import com.neco_desarrollo.myapplication.constance.Constance
import com.neco_desarrollo.myapplication.databinding.ActivitySignUpInBinding


class SignInUpAct : AppCompatActivity() {
lateinit var bindingClass: ActivitySignUpInBinding
private var signState = "empty"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bindingClass = ActivitySignUpInBinding.inflate(layoutInflater)
setContentView(bindingClass.root)

signState = intent.getStringExtra(Constance.SIGN_STATE)!!
if(signState == Constance.SIGN_IN_STATE){

bindingClass.edName.visibility = View.GONE
bindingClass.edName2.visibility = View.GONE
bindingClass.edName3.visibility = View.GONE
bindingClass.bAvatar.visibility = View.INVISIBLE

}

}

fun onClickDone(view: View){

if(signState == Constance.SIGN_UP_STATE){

val intent = Intent()
intent.putExtra(Constance.LOGIN, bindingClass.edLogin.text.toString())
intent.putExtra(Constance.PASSWORD, bindingClass.edPassword.text.toString())
intent.putExtra(Constance.NAME, bindingClass.edName.text.toString())
intent.putExtra(Constance.NAME2, bindingClass.edName2.text.toString())
intent.putExtra(Constance.NAME3, bindingClass.edName3.text.toString())
if(bindingClass.imAvatar.isVisible)intent.putExtra(Constance.AVATAR_ID, R.drawable.andrey)
setResult(RESULT_OK, intent)
finish()

} else if(signState == Constance.SIGN_IN_STATE){

intent.putExtra(Constance.LOGIN, bindingClass.edLogin.text.toString())
intent.putExtra(Constance.PASSWORD, bindingClass.edPassword.text.toString())
setResult(RESULT_OK, intent)
finish()

}

}

fun onClickAvatar(view: View){

bindingClass.imAvatar.visibility = View.VISIBLE

}



}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/mainConteiner"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/bExit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="372dp"
android:layout_marginEnd="32dp"
android:onClick="onClickSignIn"
android:text="@string/sign_in"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/bHide"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:onClick="onClickSignUp"
android:text="@string/sign_up"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="@+id/bExit"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/bExit"
app:layout_constraintTop_toBottomOf="@+id/bExit" />

<TextView
android:id="@+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/bExit"
app:layout_constraintEnd_toEndOf="@+id/bExit"
app:layout_constraintStart_toStartOf="@+id/bExit" />

<ImageView
android:id="@+id/imAvatar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/tvInfo"
app:layout_constraintEnd_toEndOf="@+id/bExit"
app:layout_constraintStart_toStartOf="@+id/bExit"
app:srcCompat="@drawable/andrey" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_sign_up_in.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".SignInUpAct">

<ImageView
android:id="@+id/imAvatar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="72dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/andrey" />

<Button
android:id="@+id/bAvatar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:onClick="onClickAvatar"
android:text="@string/avatar"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="@+id/edLogin"
app:layout_constraintStart_toStartOf="@+id/edLogin"
app:layout_constraintTop_toBottomOf="@+id/imAvatar" />

<EditText
android:id="@+id/edLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/login"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="@+id/imAvatar"
app:layout_constraintStart_toStartOf="@+id/imAvatar"
app:layout_constraintTop_toBottomOf="@+id/bAvatar" />

<EditText
android:id="@+id/edPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="@+id/edLogin"
app:layout_constraintStart_toStartOf="@+id/edLogin"
app:layout_constraintTop_toBottomOf="@+id/edLogin" />

<EditText
android:id="@+id/edName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/name"
android:inputType="textPersonName"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="@+id/edPassword"
app:layout_constraintStart_toStartOf="@+id/edPassword"
app:layout_constraintTop_toBottomOf="@+id/edPassword" />

<EditText
android:id="@+id/edName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/name2"
android:inputType="textPersonName"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="@+id/edName"
app:layout_constraintStart_toStartOf="@+id/edName"
app:layout_constraintTop_toBottomOf="@+id/edName" />

<EditText
android:id="@+id/edName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/name3"
android:inputType="textPersonName"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="@+id/edName2"
app:layout_constraintStart_toStartOf="@+id/edName2"
app:layout_constraintTop_toBottomOf="@+id/edName2" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:onClick="onClickDone"
android:text="@string/done"
app:layout_constraintEnd_toEndOf="@+id/edPassword"
app:layout_constraintStart_toStartOf="@+id/edPassword"
app:layout_constraintTop_toBottomOf="@+id/edName3" />
</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml

<resources>
<string name="app_name">MyFirstApp</string>
<string name="ok" translatable="false">OK</string>
<string name="hello">Hello</string>
<string name="sign_in" translatable="false">Sign In</string>
<string name="sign_up" translatable="false">Sign Up</string>
<string name="avatar" translatable="false">Avatar</string>
<string name="login" translatable="false">Login</string>
<string name="password" translatable="false">Password</string>
<string name="name" translatable="false">Имя</string>
<string name="name2" translatable="false">Фамилия</string>
<string name="name3" translatable="false">Отчество</string>
<string name="done" translatable="false">Готово</string>
</resources>

Constance

object Constance {

const val SIGN_IN_STATE = "sign_in"
const val SIGN_UP_STATE = "sign_up"
const val SIGN_STATE = "sign_state"
const val REQUEST_CODE_SIGN_IN = 1212
const val REQUEST_CODE_SIGN_UP = 2121

const val LOGIN = "login"
const val PASSWORD = "password"
const val NAME = "name"
const val NAME2 = "name2"
const val NAME3 = "name3"
const val AVATAR_ID = "avatar_id"

}

2 комментария для “Урок 18. Практика (Код урока)”

  1. Я тугодум, и не могу соединить Урок 18 и 25,
    Как SING_STATE засунуть в лаунчер

    Активити регистрации
    singinstate = intent.getStringExtra(Text.SING_STATE)!!
    if(singinstate == Text.SING_IN_STATE)

    —————————————————————–

    Майн Активити

    launcherLogin =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
    result: ActivityResult ->
    if (result.resultCode == RESULT_OK) {
    result.data
    val textlogin = result.data?.getStringExtra(“${Text.SING_IN_STATE}”)
    Log.d(“MyLog”, “Result = $textlogin”)

    —————————————————————–
    fun onClickLogin(view: View){

    launcherLogin?.launch(Intent(this, Registration::class.java))

    }

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *