本文用于记录自己的实验代码。
实验要求:编写带界面的简单功能计算器,具有数据输入与输出显示框,能够实现加减乘除的计算。
MainActivity代码:
package com.example.calculator;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.content.DialogInterface;
public class MainActivity extends ActionBarActivity {
private Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt10,bt11,bt12,bt13,bt14,bt15,bt16;
private EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button) findViewById(R.id.one);
bt2 = (Button) findViewById(R.id.two);
bt3 = (Button) findViewById(R.id.three);
bt4 = (Button) findViewById(R.id.four);
bt5 = (Button) findViewById(R.id.five);
bt6 = (Button) findViewById(R.id.six);
bt7 = (Button) findViewById(R.id.seven);
bt8 = (Button) findViewById(R.id.eight);
bt9 = (Button) findViewById(R.id.nine);
bt10 = (Button) findViewById(R.id.zero);
bt11= (Button) findViewById(R.id.plus);
bt12= (Button) findViewById(R.id.sub);
bt13= (Button) findViewById(R.id.multiply);
bt14= (Button) findViewById(R.id.divide);
bt15= (Button) findViewById(R.id.clear);
bt16= (Button) findViewById(R.id.equal);
ed = (EditText) findViewById(R.id.editText1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("1"); // 在 EditText 的当前文本后追加这个数字
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("2"); // 在 EditText 的当前文本后追加这个数字
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("3"); // 在 EditText 的当前文本后追加这个数字
}
});
bt4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("4"); // 在 EditText 的当前文本后追加这个数字
}
});
bt5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("5"); // 在 EditText 的当前文本后追加这个数字
}
});
bt6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("6"); // 在 EditText 的当前文本后追加这个数字
}
});
bt7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("7"); // 在 EditText 的当前文本后追加这个数字
}
});
bt8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("8"); // 在 EditText 的当前文本后追加这个数字
}
});
bt9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("9"); // 在 EditText 的当前文本后追加这个数字
}
});
bt10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("0"); // 在 EditText 的当前文本后追加这个数字
}
});
bt11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("+"); // 在 EditText 的当前文本后追加这个数字
}
});
bt12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("-"); // 在 EditText 的当前文本后追加这个数字
}
});
bt13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("*"); // 在 EditText 的当前文本后追加这个数字
}
});
bt14.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.append("/"); // 在 EditText 的当前文本后追加这个数字
}
});
bt15.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ed.setText(""); // 清空
}
});
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
bt16.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String str = ed.getText().toString();
if (str.contains("/0")) {
throw new ArithmeticException();
}
String[] plus = str.split("\\+");
double total = 0;
for(String p : plus) {
String[] minus = p.split("\\-");
double result = evaluate(minus[0]);
for(int i = 1; i < minus.length; i++) {
result -= evaluate(minus[i]);
}
total += result;
}
if (total == (int) total)
ed.setText(Integer.toString((int) total));
else ed.setText(Double.toString(total));
} catch (Exception e) {
showErrorDialog(builder);
}
}
});
}
private double evaluate(String expression) {
String[] factors = expression.split("\\*");
double product = 1;
for(String f : factors) {
String[] dividend = f.split("\\/");
double result = Double.parseDouble(dividend[0].trim());
for(int i = 1; i < dividend.length; i++) {
result /= Double.parseDouble(dividend[i].trim());
}
product *= result;
}
return product;
}
private void showErrorDialog(AlertDialog.Builder builder) {
builder.setMessage("输入错误请重试!")
.setCancelable(false)
.setPositiveButton("好的", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// To close the dialog
dialog.cancel();
}
});
// Create the alert dialog
AlertDialog alert = builder.create();
// Set the dialog title
alert.setTitle("错误");
// Show the dialog
alert.show();
}
@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);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.calculator.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/two"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/one"
android:layout_alignBottom="@+id/one"
android:layout_toRightOf="@+id/one"
android:text="2" />
<Button
android:id="@+id/three"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/two"
android:layout_toRightOf="@+id/two"
android:text="3" />
<Button
android:id="@+id/multiply"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/three"
android:layout_alignBottom="@+id/three"
android:layout_toRightOf="@+id/three"
android:text="*" />
<Button
android:id="@+id/five"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/two"
android:layout_alignTop="@+id/four"
android:text="5" />
<Button
android:id="@+id/four"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/two"
android:layout_toLeftOf="@+id/five"
android:text="4" />
<Button
android:id="@+id/six"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/three"
android:layout_alignTop="@+id/five"
android:text="6" />
<Button
android:id="@+id/divide"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/multiply"
android:layout_alignTop="@+id/six"
android:text="/" />
<Button
android:id="@+id/seven"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/four"
android:layout_below="@+id/five"
android:text="7" />
<Button
android:id="@+id/eight"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/five"
android:layout_toLeftOf="@+id/six"
android:text="8" />
<Button
android:id="@+id/nine"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/six"
android:layout_alignTop="@+id/eight"
android:text="9" />
<Button
android:id="@+id/plus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/nine"
android:layout_alignBottom="@+id/nine"
android:layout_toRightOf="@+id/six"
android:text="+" />
<Button
android:id="@+id/zero"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/seven"
android:layout_below="@+id/seven"
android:text="0" />
<Button
android:id="@+id/clear"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/zero"
android:layout_alignBottom="@+id/zero"
android:layout_alignLeft="@+id/eight"
android:text="C" />
<Button
android:id="@+id/equal"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/clear"
android:layout_alignBottom="@+id/clear"
android:layout_toRightOf="@+id/eight"
android:text="=" />
<Button
android:id="@+id/sub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/equal"
android:layout_alignBottom="@+id/equal"
android:layout_toRightOf="@+id/nine"
android:text="-" />
<Button
android:id="@+id/one"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="31dp"
android:text="1" />
</RelativeLayout>
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- 69lv.com 版权所有 湘ICP备2023021910号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务