-
Notifications
You must be signed in to change notification settings - Fork 13
Translated to Kotlin #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
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 | ||
} | ||
|
||
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 | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно упростить условие и убрать из него data != null |
||
} | ||
|
||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. потерялся private |
||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. или можно так: |
||
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 | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
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)