esto es mi primer proyecto en android studio se trata de una calculadora aunque basica espero les sirva como ejemplo .....
package com.example.calculadora;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
/**
public void onClickSuma(View miVista){
TextView temp;
temp = (TextView)findViewById(R.id.num1);
try{
float a = Float.parseFloat(temp.getText().toString());
temp = (TextView)findViewById(R.id.num2);
float b = Float.parseFloat(temp.getText().toString());
float c=a+b;
TextView resultado = (TextView)findViewById(R.id.resultado);
resultado.setText(" "+ c);
}catch (Exception e){
}
}
*/
Double num1,num2,resul;
String operador;
public void onclickcero(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "0");
}
public void onclickuno(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "1");
}
public void onclickdos(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "2");
}
public void onclicktres(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "3");
}
public void onclickcuatro(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "4");
}
public void onclickcinco(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "5");
}
public void onclickseis(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "6");
}
public void onclicksiete(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "7");
}
public void onclickocho(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "8");
}
public void onclicknueve(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText()+"9" );
}
public void onclickpunto(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + ".");
}
public void onclickdivision(View miView)
{
operador="/";
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num1 = Double.parseDouble(tv.getText().toString());
tv1.setText(num1.toString()+" / ");
tv.setText("");
}
public void onclickmultiplicacion(View miView)
{
operador="*";
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num1 = Double.parseDouble(tv.getText().toString());
tv1.setText(num1.toString()+" * ");
tv.setText("");
}
public void onclicksuma(View miView)
{
operador="+";
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num1 = Double.parseDouble(tv.getText().toString());
tv1.setText(num1.toString()+" + ");
tv.setText("");
}
public void onclickresta(View miView)
{
operador="-";
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num1 = Double.parseDouble(tv.getText().toString());
tv1.setText(num1.toString()+" - ");
tv.setText("");
}
public void onclickdelete(View miView)
{
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num2=0.0;
num1 =0.0;
tv1.setText("");
tv.setText("");
}
public void onclickigual(View miView)
{
TextView tv = (TextView) findViewById(R.id.numero) ;
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
String a = tv1.getText().toString();
num2 = Double.parseDouble(tv.getText().toString());
if (operador.equals("+")){
resul = num1 + num2;
}
else if (operador.equals("-")){
resul = num1 - num2;
}
else if (operador.equals("*")){
resul = num1 * num2;
}
else if (operador.equals("/")){
resul = num1 / num2;
}
tv1.setText(a+ num2);
tv.setText(resul.toString());
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
/**
public void onClickSuma(View miVista){
TextView temp;
temp = (TextView)findViewById(R.id.num1);
try{
float a = Float.parseFloat(temp.getText().toString());
temp = (TextView)findViewById(R.id.num2);
float b = Float.parseFloat(temp.getText().toString());
float c=a+b;
TextView resultado = (TextView)findViewById(R.id.resultado);
resultado.setText(" "+ c);
}catch (Exception e){
}
}
*/
Double num1,num2,resul;
String operador;
public void onclickcero(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "0");
}
public void onclickuno(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "1");
}
public void onclickdos(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "2");
}
public void onclicktres(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "3");
}
public void onclickcuatro(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "4");
}
public void onclickcinco(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "5");
}
public void onclickseis(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "6");
}
public void onclicksiete(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "7");
}
public void onclickocho(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + "8");
}
public void onclicknueve(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText()+"9" );
}
public void onclickpunto(View miVista){
TextView tv = (TextView) findViewById(R.id.numero) ;
tv.setText(tv.getText() + ".");
}
public void onclickdivision(View miView)
{
operador="/";
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num1 = Double.parseDouble(tv.getText().toString());
tv1.setText(num1.toString()+" / ");
tv.setText("");
}
public void onclickmultiplicacion(View miView)
{
operador="*";
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num1 = Double.parseDouble(tv.getText().toString());
tv1.setText(num1.toString()+" * ");
tv.setText("");
}
public void onclicksuma(View miView)
{
operador="+";
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num1 = Double.parseDouble(tv.getText().toString());
tv1.setText(num1.toString()+" + ");
tv.setText("");
}
public void onclickresta(View miView)
{
operador="-";
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num1 = Double.parseDouble(tv.getText().toString());
tv1.setText(num1.toString()+" - ");
tv.setText("");
}
public void onclickdelete(View miView)
{
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
TextView tv = (TextView) findViewById(R.id.numero) ;
num2=0.0;
num1 =0.0;
tv1.setText("");
tv.setText("");
}
public void onclickigual(View miView)
{
TextView tv = (TextView) findViewById(R.id.numero) ;
TextView tv1 = (TextView) findViewById(R.id.textonum) ;
String a = tv1.getText().toString();
num2 = Double.parseDouble(tv.getText().toString());
if (operador.equals("+")){
resul = num1 + num2;
}
else if (operador.equals("-")){
resul = num1 - num2;
}
else if (operador.equals("*")){
resul = num1 * num2;
}
else if (operador.equals("/")){
resul = num1 / num2;
}
tv1.setText(a+ num2);
tv.setText(resul.toString());
}
}
No hay comentarios:
Publicar un comentario