Android - Exemplo Simples
De Aulas
Links relacionados: Programação para Dispositivos Móveis
Criando o projeto
Para este exemplo basta criar um projeto utilizando um tipo básico blanc screen e deixando todos os campos como default.
strings.xml
1<resources>
2 <string name="app_name">First Application</string>
3 <string name="stringTextViewA">Text A:</string>
4 <string name="stringTextViewB">Text B:</string>
5 <string name="stringTextViewResult">Result:</string>
6 <string name="stringButtonConcat">Concat</string>
7 <string name="stringButtonClear">Clear</string>
8</resources>
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?>
2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 tools:context=".MainActivity">
8
9 <TextView
10 android:id="@+id/textViewA"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:layout_marginStart="8dp"
14 android:layout_marginTop="8dp"
15 android:text="@string/stringTextViewA"
16 app:layout_constraintStart_toStartOf="parent"
17 app:layout_constraintTop_toTopOf="parent" />
18
19 <EditText
20 android:id="@+id/editTextA"
21 android:layout_width="0dp"
22 android:layout_height="wrap_content"
23 android:layout_marginEnd="8dp"
24 android:layout_marginStart="8dp"
25 android:layout_marginTop="8dp"
26 android:ems="10"
27 android:inputType="textPersonName"
28 app:layout_constraintEnd_toEndOf="parent"
29 app:layout_constraintStart_toStartOf="parent"
30 app:layout_constraintTop_toBottomOf="@+id/textViewA" />
31
32 <TextView
33 android:id="@+id/textViewB"
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:layout_marginStart="8dp"
37 android:layout_marginTop="8dp"
38 android:text="@string/stringTextViewB"
39 app:layout_constraintStart_toStartOf="parent"
40 app:layout_constraintTop_toBottomOf="@+id/editTextA" />
41
42 <EditText
43 android:id="@+id/editTextB"
44 android:layout_width="0dp"
45 android:layout_height="wrap_content"
46 android:layout_marginEnd="8dp"
47 android:layout_marginStart="8dp"
48 android:layout_marginTop="8dp"
49 android:ems="10"
50 android:inputType="textPersonName"
51 app:layout_constraintEnd_toEndOf="parent"
52 app:layout_constraintStart_toStartOf="parent"
53 app:layout_constraintTop_toBottomOf="@+id/textViewB" />
54
55 <Button
56 android:id="@+id/buttonCat"
57 android:layout_width="wrap_content"
58 android:layout_height="wrap_content"
59 android:layout_marginStart="8dp"
60 android:layout_marginTop="8dp"
61 android:text="@string/stringButtonConcat"
62 app:layout_constraintStart_toStartOf="parent"
63 app:layout_constraintTop_toBottomOf="@+id/editTextB" />
64
65 <Button
66 android:id="@+id/buttonClear"
67 android:layout_width="wrap_content"
68 android:layout_height="wrap_content"
69 android:layout_marginEnd="8dp"
70 android:layout_marginTop="8dp"
71 android:text="@string/stringButtonClear"
72 app:layout_constraintEnd_toEndOf="parent"
73 app:layout_constraintTop_toBottomOf="@+id/editTextB" />
74
75 <EditText
76 android:id="@+id/editTextResult"
77 android:layout_width="0dp"
78 android:layout_height="wrap_content"
79 android:layout_marginEnd="8dp"
80 android:layout_marginStart="8dp"
81 android:layout_marginTop="8dp"
82 android:ems="10"
83 android:inputType="textPersonName"
84 app:layout_constraintEnd_toEndOf="parent"
85 app:layout_constraintStart_toStartOf="parent"
86 app:layout_constraintTop_toBottomOf="@+id/buttonCat" />
87</android.support.constraint.ConstraintLayout>
MainActivity.java
1package com.example.saulo.helloworld;
2
3import android.support.v7.app.AppCompatActivity;
4import android.os.Bundle;
5import android.view.View.OnClickListener;
6import android.widget.EditText;
7
8public class MainActivity extends AppCompatActivity implements OnClickListener {
9 private EditText editTextA;
10 private EditText editTextB;
11 private EditText editTextResult;
12
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17
18 editTextA = findViewById(R.id.editTextA);
19 editTextB = findViewById(R.id.editTextB);
20 editTextResult = findViewById(R.id.editTextResult);
21
22 findViewById(R.id.buttonClear).setOnClickListener(this);
23 findViewById(R.id.buttonCat).setOnClickListener(this);
24 }
25
26 @Override
27 public void onClick(View v) {
28 switch (v.getId()) {
29 case R.id.buttonCat:
30 editTextResult.setText(editTextA.getText().toString() + " " + editTextB.getText().toString());
31 break;
32 case R.id.buttonClear:
33 editTextA.setText("");
34 editTextB.setText("");
35 editTextResult.setText("");
36 break;
37 }
38 }
39}
Atividades
Faça um aplicativo para cálculo. O aplicativo deve armazenar os tipos de operações feitas e seus resultados. Ele também deve mostrar o histórico dos cálculos e deve haver um botão para desfazer os últimos cálculos executados. Veja abaixo uma sugestão de estrutura para o programa:
|