Mudanças entre as edições de "Android Kotlin - Menus e Activities"
De Aulas
(→Visual) |
|||
Linha 169: | Linha 169: | ||
<syntaxhighlight lang=kotlin n> | <syntaxhighlight lang=kotlin n> | ||
− | package com. | + | package com.example.menuteste |
import android.app.Activity | import android.app.Activity | ||
Linha 176: | Linha 176: | ||
import com.google.android.material.snackbar.Snackbar | import com.google.android.material.snackbar.Snackbar | ||
import androidx.appcompat.app.AppCompatActivity | import androidx.appcompat.app.AppCompatActivity | ||
+ | import androidx.navigation.ui.AppBarConfiguration | ||
import android.view.Menu | import android.view.Menu | ||
import android.view.MenuItem | import android.view.MenuItem | ||
− | |||
import android.widget.EditText | import android.widget.EditText | ||
import android.widget.TextView | import android.widget.TextView | ||
+ | import androidx.activity.result.contract.ActivityResultContracts | ||
+ | import com.example.menuteste.databinding.ActivityMainBinding | ||
− | + | class MainActivity : AppCompatActivity() { | |
− | + | private lateinit var appBarConfiguration: AppBarConfiguration | |
+ | private lateinit var binding: ActivityMainBinding | ||
override fun onCreate(savedInstanceState: Bundle?) { | override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | super.onCreate(savedInstanceState) | ||
− | setContentView( | + | binding = ActivityMainBinding.inflate(layoutInflater) |
− | setSupportActionBar(toolbar) | + | setContentView(binding.root) |
+ | setSupportActionBar(binding.toolbar) | ||
− | fab.setOnClickListener { view -> | + | binding.fab.setOnClickListener { view -> |
− | + | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
− | + | .setAction("Action", null).show() | |
− | |||
} | } | ||
} | } | ||
Linha 203: | Linha 206: | ||
} | } | ||
− | fun | + | private fun openOtherActivity() { |
val editTextName : EditText = findViewById(R.id.editTextName) | val editTextName : EditText = findViewById(R.id.editTextName) | ||
val editTextAge : EditText = findViewById(R.id.editTextAge) | val editTextAge : EditText = findViewById(R.id.editTextAge) | ||
Linha 210: | Linha 213: | ||
intent.putExtra("name", editTextName.text.toString()) | intent.putExtra("name", editTextName.text.toString()) | ||
intent.putExtra("age", editTextAge.text.toString()) | intent.putExtra("age", editTextAge.text.toString()) | ||
− | + | getResult.launch(intent) | |
} | } | ||
Linha 217: | Linha 220: | ||
R.id.action_settings -> true | R.id.action_settings -> true | ||
R.id.action_other -> { | R.id.action_other -> { | ||
− | + | openOtherActivity() | |
true | true | ||
} | } | ||
Linha 224: | Linha 227: | ||
} | } | ||
− | + | private val getResult = | |
− | + | registerForActivityResult( | |
− | if (resultCode == Activity.RESULT_OK) { | + | ActivityResultContracts.StartActivityForResult() |
− | + | ) { | |
− | + | if (it.resultCode == Activity.RESULT_OK) { | |
− | + | if (it.data != null) { | |
+ | val textViewMessage : TextView = findViewById(R.id.textViewMessage) | ||
+ | textViewMessage.text = it.data?.getStringExtra("message") | ||
+ | } | ||
} | } | ||
} | } | ||
− | |||
− | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Edição das 20h45min de 3 de maio de 2023
Afluentes: Dispositivos Móveis
Visual
Nesse exemplo vamos criar um projeto com um Basic Activity, mas vamos limpar ele um pouco e tirar os fragmentos. Vamos trabalhar com multiplas Activities.
Nosso exemplo pronto deve parecer com as imagens abaixo.
strings.xml
1<resources>
2 <string name="app_name">a001</string>
3 <string name="action_settings">Settings</string>
4 <string name="action_other">Other</string>
5 <string name="name">Name</string>
6 <string name="age">Age</string>
7 <string name="result">Result</string>
8 <string name="button_execute">Execute</string>
9 <string name="message">Your name is %1$s and you are %2$s years old.</string>
10</resources>
Menus
No menu_main.xml, adiciona-se os novos itens. A informação orderInCategory é a ordem que os itens do meno irão aparecer.
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<item android:id="@+id/action_other"
android:title="@string/action_other"
android:orderInCategory="101"
app:showAsAction="never"/>
MainActivity.kt
Nos programa kotlin, basta adicionar as ações dentro da opção no wen.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val myText: TextView = findViewById(R.id.textViewHello)
return when (item.itemId) {
R.id.action_settings -> {
myText.text = "Settings option"
return true
}
R.id.action_other -> {
myText.text = "Other option"
return true
}
else -> super.onOptionsItemSelected(item)
}
}
Layouts
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" android:id="@+id/textViewHello"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editTextNome"
tools:text="Nome" android:layout_marginTop="32dp" app:layout_constraintTop_toBottomOf="@+id/textViewHello"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" app:layout_constraintHorizontal_bias="0.0"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editTextIdade"
tools:text="Idade" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/editTextNome"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"/>
</android.support.constraint.ConstraintLayout>
activitie_other.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OtherActivity">
<TextView
android:text="Oi, sou a outra Activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Large" android:layout_marginTop="84dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp" android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="0.497"/>
<Button
android:text="Voltar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonVoltar" android:layout_marginTop="92dp"
app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="0.498"/>
<TextView
android:text="NOME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewNome" android:layout_marginTop="116dp"
app:layout_constraintTop_toBottomOf="@+id/buttonVoltar" app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="0.498"
android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
<TextView
android:text="IDADE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewIdade" android:layout_marginTop="104dp"
app:layout_constraintTop_toBottomOf="@+id/textViewNome" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
</android.support.constraint.ConstraintLayout>
Kotlin
MainActivity.kt
package com.example.menuteste
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.ui.AppBarConfiguration
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import com.example.menuteste.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
binding.fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
private fun openOtherActivity() {
val editTextName : EditText = findViewById(R.id.editTextName)
val editTextAge : EditText = findViewById(R.id.editTextAge)
val intent = Intent(this, OtherActivity::class.java)
intent.putExtra("name", editTextName.text.toString())
intent.putExtra("age", editTextAge.text.toString())
getResult.launch(intent)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_settings -> true
R.id.action_other -> {
openOtherActivity()
true
}
else -> super.onOptionsItemSelected(item)
}
}
private val getResult =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == Activity.RESULT_OK) {
if (it.data != null) {
val textViewMessage : TextView = findViewById(R.id.textViewMessage)
textViewMessage.text = it.data?.getStringExtra("message")
}
}
}
}
OtherActivity.kt
package com.arisa.exemplo
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
class OtherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_other)
val textViewName : TextView = findViewById(R.id.textViewName)
val textViewAge : TextView = findViewById(R.id.textViewAge)
val bundle = intent.extras
if (bundle != null) {
textViewName.text = bundle.getString("name")
textViewAge.text = bundle.getString("age")
}
val buttonExecute : Button = findViewById(R.id.buttonExecute)
buttonExecute.setOnClickListener {
val out = getString(R.string.message, textViewName.text, textViewAge.text)
val data = Intent()
data.putExtra("result", out)
setResult(RESULT_OK, data)
finish()
}
}
}