Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
}
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
Expand All @@ -25,6 +26,9 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures{
viewBinding true
}
}

dependencies {
Expand All @@ -35,4 +39,9 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
7 changes: 2 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.KotlinHomeWork">
<activity android:name=".MainActivity">
<activity android:name=".NewSecondActivity"></activity>
<activity android:name=".NewMainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:exported="false"
android:windowSoftInputMode="adjustResize" />
</application>

</manifest>
69 changes: 0 additions & 69 deletions app/src/main/java/com/example/kotlinhomework/MainActivity.java

This file was deleted.

62 changes: 62 additions & 0 deletions app/src/main/java/com/example/kotlinhomework/NewMainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.example.kotlinhomework

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlinhomework.databinding.ActivityNewMainBinding

class NewMainActivity : AppCompatActivity() {

private lateinit var greetings: String
private lateinit var name: String
private lateinit var textView: TextView

private val binding: ActivityNewMainBinding by lazy {
val tmpBinding = ActivityNewMainBinding.inflate(layoutInflater)
setContentView(tmpBinding.root)
tmpBinding
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

greetings = getString(R.string.hello)
name = getString(R.string.anon)

savedInstanceState?.getString(NAME_KEY)?.let {
name = it
}
Comment on lines +27 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Наверное не имеет смысла предварительно получать R.string.anon
можно записать это более лаконично, используя оператор Элвис:
name = savedInstanceState?.getString(NAME_KEY) ?: getString(R.string.anon)


textView = binding.textViewHello
binding.buttonNameYourSelf.setOnClickListener {
startActivityForResult(Intent(this, NewSecondActivity::class.java), NewSecondActivity.GET_NAME_REQUEST_CODE)
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == NewSecondActivity.GET_NAME_REQUEST_CODE && resultCode == RESULT_OK && data != null){
data.getStringExtra((NewSecondActivity.NAME_KEY))?.let{
name = it
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно упростить условие и убрать из него data != null
вместо этого воспользуемся безопасным вызовом: ?.
data?.getStringExtra(NewSecondActivity.NAME_KEY)?.let{ name = it }
и лишние двойные скобки

}

override fun onResume() {
super.onResume()
textView.text = getString(R.string.format_string, greetings, name)
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(NAME_KEY, name)
}


companion object {
const val NAME_KEY = "com.example.kotlinhomework.MainActivity.NAME_KEY"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

потерялся private

}
}
63 changes: 63 additions & 0 deletions app/src/main/java/com/example/kotlinhomework/NewSecondActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.example.kotlinhomework

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Button
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
import com.example.kotlinhomework.databinding.ActivityNewMainBinding
import com.example.kotlinhomework.databinding.ActivityNewSecondBinding

class NewSecondActivity : AppCompatActivity() {

private lateinit var editText: EditText
private lateinit var button: Button

private val binding: ActivityNewSecondBinding by lazy {
val tmpBinding = ActivityNewSecondBinding.inflate(layoutInflater)
setContentView(tmpBinding.root)
tmpBinding
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

editText = binding.editTextTextPersonName
button = binding.buttonName
button.isEnabled = false
editText.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Nothing to do
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// Nothing to do
}

override fun afterTextChanged(s: Editable?) {
s?.let {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

или можно так:
if (s.isNullOrEmpty().not()) button.isEnabled = true

if(it.isNotEmpty()) button.isEnabled = true
}

}

})

button.setOnClickListener {
val intent = Intent().apply {
putExtra(NAME_KEY, editText.text.toString())
}
setResult(RESULT_OK, intent)
finish()
}
}

companion object {
const val NAME_KEY = "com.example.kotlinhomework.SecondActivity.NAME_KEY"
const val GET_NAME_REQUEST_CODE = 1234
}
}
57 changes: 0 additions & 57 deletions app/src/main/java/com/example/kotlinhomework/SecondActivity.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
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=".MainActivity">
android:layout_height="match_parent">

<TextView
android:id="@+id/textViewHello"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<string name="anon">Anon</string>
<string name="enter_name">Enter your name</string>
<string name="my_name">That\'s my name!</string>
<string name="format_string">%s, %s!</string>
</resources>
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.4.21'
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0-alpha02"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down