Mudanças entre as edições de "Android Kotlin - Menus e Activities"
De Aulas
Linha 1: | Linha 1: | ||
Afluentes: [[Dispositivos Móveis]] | Afluentes: [[Dispositivos Móveis]] | ||
+ | |||
+ | = Visual = | ||
+ | |||
+ | <center>[[Image:Android_studio_menus_activities.png|500px]]</center> | ||
= strings.xml = | = strings.xml = |
Edição das 14h37min de 3 de maio de 2022
Afluentes: Dispositivos Móveis
Visual
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.arisa.exemplo
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 android.view.Menu
import android.view.MenuItem
import android.widget.ActionMenuView
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener { view ->
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show()
abreOutraJanela()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
fun abreOutraJanela() {
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())
startActivityForResult(intent, 1)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_settings -> true
R.id.action_other -> {
abreOutraJanela()
true
}
else -> super.onOptionsItemSelected(item)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
val textViewResult : TextView = findViewById(R.id.textViewResult)
textViewResult.text = data.getStringExtra("result")
}
}
}
}
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()
}
}
}