PRÁTICA 7 – EDITTEXT
Objetivo:
Utilizar editText para entrar com texto e formatar
Introdução
editText -> Entrada de texto do usuário na tela.
Tarefa:
Criar tipos aplicações de textView e buttons:
Programa 1 – Design padrão com um textView e um editView:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/texto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="false" android:text="Dados" android:layout_marginTop="14dp" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/angulo" android:layout_below="@+id/texto" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:numeric="signed|decimal" /> <TextView android:id="@+id/saida" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="false" android:text="saida" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_centerHorizontal="true" android:layout_below="@+id/angulo" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calcula" android:id="@+id/calcula" android:layout_below="@+id/saida" android:layout_centerHorizontal="true" /> </RelativeLayout> |
Programa 1 – Calculadora de cossenos:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView saida =(TextView)findViewById(R.id.saida); final EditText angulo = (EditText) findViewById(R.id.angulo); final Button calcula = (Button) findViewById(R.id.calcula); calcula.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double cos = Math.cos(Double.valueOf(angulo.getText().toString())); saida.setText("COS = "+String.valueOf(cos)); } }); } } |
Programa 2 – Linear Vertical LayOut com dois editTexts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="empty.ammsoft.empty.MainActivity" android:id="@+id/layout" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Triângulo de Pitágoras" android:id="@+id/textView4" android:layout_gravity="center_horizontal" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="a" android:id="@+id/textView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/a" android:layout_weight="1" android:inputType="number|numberSigned" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="b" android:id="@+id/textView3" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/b" android:layout_weight="1" android:inputType="number|numberSigned" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="C=" android:id="@+id/x1" android:layout_weight="1" /> </LinearLayout> </LinearLayout> |
Programa 2 – OnFocusChangeListener:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { public void calcula() { final EditText a_edit = (EditText) findViewById(R.id.a); final EditText b_edit = (EditText) findViewById(R.id.b); final TextView c_text = (TextView) findViewById(R.id.c); double a = Double.valueOf(a_edit.getText().toString()); double b = Double.valueOf(b_edit.getText().toString()); double c = Math.sqrt(a*a + b*b); c_text.setText(String.format("%.2f", c)); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText a_edit = (EditText) findViewById(R.id.a); final EditText b_edit = (EditText) findViewById(R.id.b); a_edit.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { calcula(); } }); b_edit.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { calcula(); } }); } } |
Desafio: Implemente a mesma ideia para calcular o valor corrigido por juros compostos mensais depois de n meses:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="empty.ammsoft.empty.MainActivity" android:id="@+id/layout" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Juros Compostos" android:id="@+id/textView4" android:layout_gravity="center_horizontal" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentBottom="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Inicial (R$)" android:id="@+id/textView" android:layout_weight="1" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/valorInicial" android:layout_weight="1" android:inputType="number|numberSigned" android:text="1000" android:numeric="integer" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Juros (aa %)" android:id="@+id/textView2" android:layout_weight="1" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/juros" android:layout_weight="1" android:inputType="number|numberSigned" android:text="8" android:numeric="integer" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Meses" android:id="@+id/textView3" android:layout_weight="1" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/meses" android:layout_weight="1" android:inputType="number|numberSigned" android:text="48" android:numeric="integer" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Final" android:id="@+id/valorFinal" android:layout_weight="1" /> </LinearLayout> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package nome do seu pacote! // Esta linha deve ser mantida com o nome do pacote! import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { EditText valorInicial; EditText juros; EditText meses; TextView valorFinal; public void calculaJuros() { double valorInicialNum = Double.valueOf(valorInicial.getText().toString()); double jurosNumAA = Double.valueOf(juros.getText().toString()); double mesesNum = Double.valueOf(meses.getText().toString()); double jurosNumAM = Math.pow(1+jurosNumAA/100d,1d/12d); double valorFinalNum = valorInicialNum*Math.pow(jurosNumAM,mesesNum); valorFinal.setText("Valor final = R$"+String.format("%.2f", valorFinalNum)); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); valorInicial = (EditText) findViewById(R.id.valorInicial); juros = (EditText) findViewById(R.id.juros); meses = (EditText) findViewById(R.id.meses); valorFinal = (TextView) findViewById(R.id.valorFinal); valorInicial.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { calculaJuros(); } }); juros.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { calculaJuros(); } }); meses.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { calculaJuros(); } }); } } |