package com.example.teste;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.graphics.Color;
import android.view.View;;
public class MainActivity extends ActionBarActivity {
EditText e ;
EditText e1;
EditText t;
Button b1,b2,b3,b4,b5,b6,b7;
Double r,r1,r2;
AlertDialog.Builder menssagem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e = (EditText)findViewById(R.id.editText1);
e1 = (EditText)findViewById(R.id.editText2);
t = (EditText)findViewById(R.id.resultado);
t.setEnabled(false);
menssagem = new AlertDialog.Builder(MainActivity.this);
}
@Override
protected void onDestroy() {
super.onDestroy();
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void somar(View v ){
// TODO Auto-generated method stub
r = Double.parseDouble(e.getText().toString());
r1= Double.parseDouble(e1.getText().toString());
r2= r+r1;
menssagem.setTitle("Resultado da Soma");
menssagem.setMessage(String.valueOf(r2));
menssagem.setNeutralButton("OK",null);
menssagem.show();
t.setText(""+r2);
}
public void sub(View v ){
r = Double.parseDouble(e.getText().toString());
r1= Double.parseDouble(e1.getText().toString());
r2= r-r1;
menssagem.setTitle("Resultado da Subtração");
menssagem.setMessage(String.valueOf(r2));
menssagem.setNeutralButton("OK",null);
menssagem.show();
t.setText(""+r2);
}
public void mult(View v ){
r = Double.parseDouble(e.getText().toString());
r1 = Double.parseDouble(e1.getText().toString());
r2 = r*r1;
menssagem.setTitle("Resultado da Multiplicacão");
menssagem.setMessage(String.valueOf(r2));
menssagem.setNeutralButton("OK",null);
menssagem.show();
t.setText(""+r2);
}
public void div(View v ){
r = Double.parseDouble(e.getText().toString());
r1 = Double.parseDouble(e1.getText().toString());
r2 = r/r1;
menssagem.setTitle("Resultado da Divisão");
menssagem.setMessage(String.valueOf(r2));
menssagem.setNeutralButton("OK",null);
menssagem.show();
t.setText(""+r2);
}
public void perc(View v ){
r = Double.parseDouble(e.getText().toString());
r1 = Double.parseDouble(e1.getText().toString());
r2 = (r*r1)/100;
menssagem.setTitle("Resuldado de "+r+"% de "+r1);
menssagem.setMessage(String.valueOf(r2));
menssagem.setNeutralButton("OK",null);
menssagem.show();
t.setText(""+r2);
}
public void limp(View v ){
e.setText("");
e1.setText("");
t.setText("");
}
public void sair(View v ){
finish();
}
}
activitymain.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.example.teste.MainActivity" >
<TextView
android:id="@+id/texto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/texto" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:digits="1234567890."
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/texto1" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:digits="1234567890."
android:ems="10" >
</EditText>
<TextView
android:id="@+id/result1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/texto2" />
<EditText
android:id="@+id/resultado"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.01"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="+"
android:onClick="somar"/>
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="-"
android:onClick="sub"/>
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignLeft="@+id/button4"
android:text="/"
android:onClick="div"/>
<Button
android:id="@+id/button4"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="*"
android:onClick="mult"/>
<Button
android:id="@+id/button5"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:text="%"
android:onClick="perc"/>
<Button
android:id="@+id/button6"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/button7"
android:text="limpar"
android:onClick="limp"/>
<Button
android:id="@+id/button7"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="sair"
android:onClick="sair"/>
</RelativeLayout>
</LinearLayout>
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Calculadora</string>
<string name="texto">Valor 1:</string>
<string name="texto1">Valor 2:</string>
<string name="texto2">Resultado:</string>
<string name="Button">=</string>
<string name="action_settings">Settings</string>
</resources>
Nenhum comentário:
Postar um comentário