您的位置:首页 > 移动开发 > Android开发

关于Android中计算器的设计

2017-05-09 07:14 423 查看
最近需要完成一个简单计算器的设计,思索了之后完成了一下的设计。

首先进行Java类的编写,实现计算中的相应从操作。
进行页面的设计,(这里我编写了两个界面,普通和高级)。
两个页面之间进行跳转。
点击产生不同的操作结果。

首先进行主界面的设计:
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/lay"
>

<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:textColor="#72DEFD"
android:textSize="20pt"
android:background="#F2F2F2"
android:gravity="right|center_vertical"
android:id="@+id/show"
/>

<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
>
<Button
android:id="@+id/left"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:textSize="13pt"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:paddingBottom="7dp"
android:text="高级"
/>
<Button
android:id="@+id/right"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:paddingBottom="7dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:text="()"
/>
<Button
android:id="@+id/delete"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:text="C"
/>

<Button
android:id="@+id/back"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="13pt"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:text="清除" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
>
<Button
android:id="@+id/b7"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="7" />

<Button
android:id="@+id/b8"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="8"  />

<Button
android:id="@+id/b9"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="9" />

<Button
android:id="@+id/chu"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="÷" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
>
<Button
android:id="@+id/b4"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="4" />

<Button
android:id="@+id/b5"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="5"  />

<Button
android:id="@+id/b6"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="6" />

<Button
android:id="@+id/chen"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="×" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
>
<Button
android:id="@+id/b1"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="1" />

<Button
android:id="@+id/b2"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="2"  />

<Button
android:id="@+id/b3"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="3" />

<Button
android:id="@+id/jian"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textCol
133cf
or="#FFF"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="-" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
>
<Button
android:id="@+id/dian"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="." />

<Button
android:id="@+id/b0"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="0"  />

<Button
android:id="@+id/dengyu"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="=" />

<Button
android:id="@+id/jia"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="+" />
</TableRow>
</TableLayout>
</LinearLayout>


进行高级界面的设计:
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/lay"
>

<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:textColor="#72DEFD"
android:textSize="20pt"
android:background="#F2F2F2"
android:gravity="right|center_vertical"
android:id="@+id/show"
/>

<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
>
<Button
android:id="@+id/m"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:textSize="16pt"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:paddingBottom="7dp"
android:text="㎡"/>
<Button
android:id="@+id/jiechen"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:textSize="13pt"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:paddingBottom="7dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:text="x!"
/>
<Button
android:id="@+id/clear"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:text="C"
/>

<Button
android:id="@+id/back"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="12pt"
android:layout_marginLeft="1dp"
android:layout_marginBottom="1dp"
android:text="清除" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
>
<Button
android:id="@+id/b7"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="7" />

<Button
android:id="@+id/b8"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="8"  />

<Button
android:id="@+id/b9"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="9" />

<Button
android:id="@+id/sin"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="12pt"
android:layout_marginBottom="1dp"
android:text="sin" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
>
<Button
android:id="@+id/b4"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="4" />

<Button
android:id="@+id/b5"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="5"  />

<Button
android:id="@+id/b6"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="6" />

<Button
android:id="@+id/cos"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="12pt"
android:layout_marginBottom="1dp"
android:text="cos" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
>
<Button
android:id="@+id/b1"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="1" />

<Button
android:id="@+id/b2"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="2"  />

<Button
android:id="@+id/b3"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="3" />

<Button
android:id="@+id/tan"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="12pt"
android:layout_marginBottom="1dp"
android:text="tan" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
>
<Button
android:id="@+id/dian"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="." />

<Button
android:id="@+id/b0"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="0"  />

<Button
android:id="@+id/dengyu"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#FFFFFF"
android:textColor="#24A9E1"
android:textSize="20pt"
android:layout_marginBottom="1dp"
android:text="=" />

<Button
android:id="@+id/re"
android:layout_height="fill_parent"
android:layout_weight="0.25"
android:layout_marginLeft="1dp"
android:background="#24A9E1"
android:textColor="#FFF"
android:textSize="12pt"
android:layout_marginBottom="1dp"
android:text="返回" />
</TableRow>
</TableLayout>
</LinearLayout>


Java类编写:
package k.caculator;

import java.math.BigDecimal;

/**
* Created by Administrator on 2017/4/1.
*/
public class clu {
BigDecimal number1;
BigDecimal number2;

//sub
BigDecimal clulatesub(String h1,String h2){
number1 = new BigDecimal(h1);
number2 = new BigDecimal(h2);
return number1.subtract(number2);
}
//add
BigDecimal clulateadd(String h1,String h2){
number1 = new BigDecimal(h1);
number2 = new BigDecimal(h2);
return number1.add(number2);
}
//multiply
BigDecimal clulatemultiply(String h1,String h2){
number1 = new BigDecimal(h1);
number2 = new BigDecimal(h2);
return number1.multiply(number2);
}
//divde
BigDecimal clulatedivde(String h1,String h2){
number1 = new BigDecimal(h1);
number2 = new BigDecimal(h2);
return number1.divide(number2);
}
//平方
BigDecimal clulatem(String h1){
number1 = new BigDecimal(h1);
return number1.multiply(number1);
}
//阶乘
double clulatex(String h1){
int i = Integer.valueOf(h1).intValue();
double result=1;
while(i>=1){
result = i*result;
i--;
}
return result;
}
//sin
double clulatesin(String h1){
double i = Integer.valueOf(h1).intValue();
double result=0;
result = Math.sin(i);
return result;
}
//cos
double clulatecos(String h1){
double i = Integer.valueOf(h1).intValue();
double result=0;
result = Math.cos(i);
return result;
}
//tan
double clulatetan(String h1){
double i = Integer.valueOf(h1).intValue();
double result=0;
result = Math.tan(i);
return result;
}
}


Java文件编写。
package k.caculator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

TextView rsText = null;  //显示器
String  num1, num2;
BigDecimal Result;  //计算结果
int op = 0; //判断操作数,
boolean isClickEqu = false; //判断是否按了“=”按钮
clu haha = new clu();//定义类实例

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

//默认软键盘不弹出
//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

//让editText失去焦点,使用EditText的clearFocus方法
//        EditText edit = (EditText)findViewById(R.id.show);
//        edit.clearFocus();

//        //让软键盘始终不显示
//        EditText edit = (EditText)findViewById(R.id.show);
//        edit.setInputType(InputType.TYPE_NULL);

//功能按钮
rsText = (TextView) findViewById(R.id.show);
Button b_delete = (Button) findViewById(R.id.delete);
Button b_back = (Button) findViewById(R.id.back);
Button b_jia = (Button) findViewById(R.id.jia);
Button b_jian = (Button) findViewById(R.id.jian);
Button b_cheng = (Button) findViewById(R.id.chen);
Button b_chu = (Button) findViewById(R.id.chu);
Button b_dengyu = (Button) findViewById(R.id.dengyu);
Button b_dian = (Button) findViewById(R.id.dian);

//高级按钮
Button bt = (Button) findViewById(R.id.left);

bt.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(MainActivity.this,second.class);
MainActivity.this.startActivity(intent);
}
});

//数字按钮
Button b0 = (Button) findViewById(R.id.b0);
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
Button b3 = (Button) findViewById(R.id.b3);
Button b4 = (Button) findViewById(R.id.b4);
Button b5 = (Button) findViewById(R.id.b5);
Button b6 = (Button) findViewById(R.id.b6);
Button b7 = (Button) findViewById(R.id.b7);
Button b8 = (Button) findViewById(R.id.b8);
Button b9 = (Button) findViewById(R.id.b9);

//listener
rsText.setOnClickListener(this);
b_delete.setOnClickListener(this);
b_back.setOnClickListener(this);
b_jia.setOnClickListener(this);
b_jian.setOnClickListener(this);
b_cheng.setOnClickListener(this);
b_chu.setOnClickListener(this);
b_dengyu.setOnClickListener(this);
b_dian.setOnClickListener(this);

b0.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
//AC和back
case R.id.back:
String myStr = rsText.getText().toString();
try {
rsText.setText(myStr.substring(0, myStr.length() - 1));
} catch (Exception e) {
rsText.setText("");
}
break;
case R.id.delete:
rsText.setText(null);
break;
//b0--9
case R.id.b0:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString = rsText.getText().toString();
myString += "0";
rsText.setText(myString);
break;
case R.id.b1:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString1 = rsText.getText().toString();
myString1 += "1";
rsText.setText(myString1);
break;
case R.id.b2:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString2 = rsText.getText().toString();
myString2 += "2";
rsText.setText(myString2);
break;
case R.id.b3:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString3 = rsText.getText().toString();
myString3 += "3";
rsText.setText(myString3);
break;
case R.id.b4:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString4 = rsText.getText().toString();
myString4 += "4";
rsText.setText(myString4);
break;
case R.id.b5:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString5 = rsText.getText().toString();
myString5 += "5";
rsText.setText(myString5);
break;
case R.id.b6:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString6 = rsText.getText().toString();
myString6 += "6";
rsText.setText(myString6);
break;
case R.id.b7:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString7 = rsText.getText().toString();
myString7 += "7";
rsText.setText(myString7);
break;
case R.id.b8:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString8 = rsText.getText().toString();
myString8 += "8";
rsText.setText(myString8);
break;
case R.id.b9:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString9 = rsText.getText().toString();
myString9 += "9";
rsText.setText(myString9);
break;
case R.id.dian:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString0 = rsText.getText().toString();
myString0 += ".";
rsText.setText(myString0);
break;
//b+,-,=
case R.id.jia:
String myStringAdd = rsText.getText().toString();
if (myStringAdd.equals(null)) {
return;
}
num1 = myStringAdd;
rsText.setText(null);
op = 1;
isClickEqu = false;
break;
case R.id.jian:
String myStringSub = rsText.getText().toString();
if (myStringSub.equals(null)) {
return;
}
num1 = myStringSub;
rsText.setText(null);
op = 2;
isClickEqu = false;
break;
case R.id.chen:
String myStringMul = rsText.getText().toString();
if (myStringMul.equals(null)) {
return;
}
num1 = myStringMul;
rsText.setText(null);
op = 3;
isClickEqu = false;
break;
case R.id.chu:
String myStringDiv = rsText.getText().toString();
if (myStringDiv.equals(null)) {
return;
}
num1 = myStringDiv;
rsText.setText(null);
op = 4;
isClickEqu = false;
break;
case R.id.dengyu:
String myStringEqu = rsText.getText().toString();
if (myStringEqu.equals(null)) {
return;
}
num2 = myStringEqu;
rsText.setText(null);
switch (op) {
case 0:
double i = Integer.valueOf(num2).intValue();
Result = BigDecimal.valueOf(i);
break;
case 1:
Result = haha.clulateadd(num1,num2);
break;
case 2:
Result = haha.clulatesub(num1,num2);;
break;
case 3:
Result = haha.clulatemultiply(num1,num2);;
break;
case 4:
double num2ch = Integer.valueOf(num2).intValue();
if(num2ch==0){
rsText.setText("格式输入错误!");
return;
}
Result = haha.clulatedivde(num1,num2);;
break;
default:
Result = BigDecimal.valueOf('0');
break;
}
rsText.setText(String.valueOf(Result));
isClickEqu = true;
break;
default:
break;
}
}

//定义一个变量,来表示是否退出
private static boolean isExit = false;
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
isExit = false;
}
};
//点击两次返回键退出
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
return false;
}
return super.onKeyDown(keyCode, event);
}
//点击两次返回键退出
private void exit() {
if (!isExit) {
isExit = true;
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
// 利用handler延迟发送更改状态信息
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
finish();
System.exit(0);
}
}
}
//高级界面的Java文件
package k.caculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.math.BigDecimal;

public class second extends AppCompatActivity  implements View.OnClickListener{
TextView rsText = null;  //显示器
String  num1, num2;
BigDecimal Result;  //计算结果
int op = 0; //判断操作数,
boolean isClickEqu = false; //判断是否按了“=”按钮
clu haha = new clu();//定义类实例
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

//功能按钮
rsText = (TextView) findViewById(R.id.show);
Button b_clear = (Button) findViewById(R.id.clear);//不能够清除
Button b_back = (Button) findViewById(R.id.back);
Button b_m = (Button) findViewById(R.id.m);
Button b_x = (Button) findViewById(R.id.jiechen);
Button b_sin = (Button) findViewById(R.id.sin);
Button b_cos = (Button) findViewById(R.id.cos);
Button b_tan = (Button) findViewById(R.id.tan);
Button b_dian = (Button) findViewById(R.id.dian);
Button b_dengyu = (Button) findViewById(R.id.dengyu);

//返回按钮
Button b_re = (Button) findViewById(R.id.re);

//返回按钮功能
b_re.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(second.this,MainActivity.class);
second.this.startActivity(intent);
}
});

//数字按钮
Button b0 = (Button) findViewById(R.id.b0);
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
Button b3 = (Button) findViewById(R.id.b3);
Button b4 = (Button) findViewById(R.id.b4);
Button b5 = (Button) findViewById(R.id.b5);
Button b6 = (Button) findViewById(R.id.b6);
Button b7 = (Button) findViewById(R.id.b7);
Button b8 = (Button) findViewById(R.id.b8);
Button b9 = (Button) findViewById(R.id.b9);

//listener
rsText.setOnClickListener(this);
b_m.setOnClickListener(this);
b_back.setOnClickListener(this);
b_sin.setOnClickListener(this);
b_cos.setOnClickListener(this);
b_tan.setOnClickListener(this);
b_x.setOnClickListener(this);
b_dengyu.setOnClickListener(this);
b_dian.setOnClickListener(this);

b0.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
//AC和back
case R.id.back:
String myStr = rsText.getText().toString();
try {
rsText.setText(myStr.substring(0, myStr.length() - 1));
} catch (Exception e) {
rsText.setText("");
}
break;
case R.id.clear:
rsText.setText(null);
break;
//b0--9
case R.id.b0:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString = rsText.getText().toString();
myString += "0";
rsText.setText(myString);
break;
case R.id.b1:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString1 = rsText.getText().toString();
myString1 += "1";
rsText.setText(myString1);
break;
case R.id.b2:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString2 = rsText.getText().toString();
myString2 += "2";
rsText.setText(myString2);
break;
case R.id.b3:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString3 = rsText.getText().toString();
myString3 += "3";
rsText.setText(myString3);
break;
case R.id.b4:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString4 = rsText.getText().toString();
myString4 += "4";
rsText.setText(myString4);
break;
case R.id.b5:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString5 = rsText.getText().toString();
myString5 += "5";
rsText.setText(myString5);
break;
case R.id.b6:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString6 = rsText.getText().toString();
myString6 += "6";
rsText.setText(myString6);
break;
case R.id.b7:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString7 = rsText.getText().toString();
myString7 += "7";
rsText.setText(myString7);
break;
case R.id.b8:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString8 = rsText.getText().toString();
myString8 += "8";
rsText.setText(myString8);
break;
case R.id.b9:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString9 = rsText.getText().toString();
myString9 += "9";
rsText.setText(myString9);
break;
case R.id.dian:
if (isClickEqu) {
rsText.setText(null);
isClickEqu = false;
}
String myString0 = rsText.getText().toString();
myString0 += ".";
rsText.setText(myString0);
break;
case R.id.m:
String myStringm = rsText.getText().toString();
if (myStringm.equals(null)) {
return;
}
num1 = myStringm;
rsText.setText(myStringm + "^2");
op = 1;
isClickEqu = false;
break;
case R.id.jiechen:
String myStringx = rsText.getText().toString();
if (myStringx.equals(null)) {
return;
}
num1 = myStringx;
rsText.setText(myStringx + "!");
op = 2;
isClickEqu = false;
break;
case R.id.sin:
String myStringsin = rsText.getText().toString();
if (myStringsin.equals(null)) {
return;
}
num1 = myStringsin;
rsText.setText("sin("+myStringsin + ")");
op = 3;
isClickEqu = false;
break;
case R.id.cos:
String myStringcos = rsText.getText().toString();
if (myStringcos.equals(null)) {
return;
}
num1 = myStringcos;
rsText.setText("cos("+myStringcos + ")");
op = 4;
isClickEqu = false;
break;
case R.id.tan:
String myStringtan = rsText.getText().toString();
if (myStringtan.equals(null)) {
return;
}
num1 = myStringtan;
rsText.setText("cos("+myStringtan + ")");
op = 5;
isClickEqu = false;
break;
case R.id.dengyu:
String myStringEqu = rsText.getText().toString();
if (myStringEqu.equals(null)) {
return;
}
num2 = myStringEqu;
rsText.setText(null);
switch (op) {
case 0://本身
double i = Integer.valueOf(num2).intValue();
Result = BigDecimal.valueOf(i);
break;
case 1://平方
Result = haha.clulatem(num1);
break;
case 2://阶乘
rsText.setText(String.valueOf(haha.clulatex(num1)));
return;
case 3:			   			             	rsText.setText(String.valueOf(haha.clulatesin(num1)));;
return;
case 4:
rsText.setText(String.valueOf(haha.clulatecos(num1)));
return;
case 5:
rsText.setText(String.valueOf(haha.clulatetan(num1)));
return;
default:
Result = BigDecimal.valueOf('0');
break;
}
rsText.setText(String.valueOf(Result));
isClickEqu = true;
break;
default:
break;
}
}
}


经过这样的设计之后就能够得到一个能够进行简单计算的计算器。


由于本人也是初学者,所以有得很么不对的地方,还请各位大牛不吝赐教。如果需要源码的朋友,请私信我!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 设计