Calculon :-)

Submitted by gary on Sat, 02/23/2013 - 01:00

Щойно за уроком http://startandroid.ru/ru/uroki/vse-uroki-spiskom/54-urok-19-pishem-prostoj-kalkuljator.html створив простий калькулятор. Дуже порадувало.

Макет:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:layout_marginLeft="10pt"
android:layout_marginRight="10pt"
android:layout_marginTop="3pt">
<EditText
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:id="@+id/etNum1"
android:layout_width="match_parent"
android:inputType="numberDecimal">
</EditText>
     <EditText
       android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5pt"
android:id="@+id/etNum2"
android:layout_width="match_parent"
android:inputType="numberDecimal">
    </EditText>
  </LinearLayout>
  <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:layout_marginTop="3pt"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt">
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="8pt"
android:id="@+id/btnAdd">
    </Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="8pt"
android:id="@+id/btnSub">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="*"
android:textSize="8pt"
android:id="@+id/btnMul">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="/"
android:textSize="8pt"
android:id="@+id/btnDiv">
</Button>
  </LinearLayout>
  <TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt"
android:textSize="12pt"
android:layout_marginTop="3pt"
android:id="@+id/tvResult"
android:gravity="center_horizontal">
  </TextView>
</LinearLayout>

Код:

package ua.pp.garik.android.das_calc;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

final int MENU_RESET_ID = 1;
final int MENU_QUIT_ID = 2;

EditText etNum1, etNum2;
Button btnAdd, btnSub, btnMul, btnDiv;
TextView tvResult;
String oper = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etNum1 = (EditText)findViewById(R.id.etNum1);
etNum2 = (EditText)findViewById(R.id.etNum2);
btnAdd = (Button)findViewById(R.id.btnAdd);
btnSub = (Button)findViewById(R.id.btnSub);
btnMul = (Button)findViewById(R.id.btnMul);
btnDiv = (Button)findViewById(R.id.btnDiv);
tvResult = (TextView)findViewById(R.id.tvResult);

btnAdd.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(0, MENU_RESET_ID, 0, "Reset");
menu.add(0, MENU_QUIT_ID, 0, "Quit");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_RESET_ID:
etNum1.setText("");
etNum2.setText("");
tvResult.setText("");
break;
case MENU_QUIT_ID:
finish();
break;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
//onClick пишеться з маленької літери!!!!

float num1 = 0;
float num2 = 0;
float res = 0;

if (TextUtils.isEmpty(etNum1.getText().toString()) || TextUtils.isEmpty(etNum2.getText().toString())) {
tvResult.setText(R.string.error_empty_fields);
return;
}

num1 = Float.parseFloat(etNum1.getText().toString());
num2 = Float.parseFloat(etNum2.getText().toString());

switch (v.getId()) {
case R.id.btnAdd:
oper = "+";
res = num1 + num2;
break;
case R.id.btnSub:
oper = "-";
res = num1 - num2;
break;
case R.id.btnMul:
oper = "*";
res = num1 * num2;
break;
case R.id.btnDiv:
if (num2 == 0) {
tvResult.setText(R.string.error_subzero);
return;
}
oper = "/";
res = num1 / num2;
break;
default:
break;
}
tvResult.setText(num1 + " " + oper + " " + num2 + " = " + res);
}
}

Скріни: