简易计算器

这是一个简单的计算器,没有经过完整的测试,能进行一些简单运算,没有输入错 误检查

,发到博客只是为了保存资料。不喜勿喷。

接下来是布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:id="@+id/edit_result"
android:layout_width="match_parent"
android:layout_height="125dp"
android:layout_weight="0.05"
android:textSize="30sp"
android:gravity="fill"
/>
<TextView
android:id="@+id/tex_result"
android:layout_width="match_parent"
android:layout_height="125dp"
android:textSize="30sp"
android:gravity="fill"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="70dp">

<Button
android:id="@+id/button_AC"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="AC"
android:textColor="#FFA07A"
android:textSize="25sp" />
<Button
android:id="@+id/button_delete"
android:layout_weight="0.25"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="←"
android:textSize="25sp"/>
<Button
android:id="@+id/button_division"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
android:text="÷"
android:textSize="25sp"/>
<Button
android:id="@+id/button_multiplication"
android:layout_weight="0.25"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="×"
android:textSize="25sp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="70dp">
<Button
android:id="@+id/button_7"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
android:text="7"
android:textSize="25sp"/>
<Button
android:id="@+id/button_8"
android:layout_weight="0.25"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="8"
android:textSize="25sp"/>
<Button
android:id="@+id/button_9"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
android:text="9"
android:textSize="25sp"/>
<Button
android:id="@+id/button_reduce"
android:layout_weight="0.25"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="-"
android:textSize="40sp"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="70dp">
<Button
android:id="@+id/button_4"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
android:text="4"
android:textSize="25sp"/>

<Button
android:id="@+id/button_5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="5"
android:textSize="25sp" />
<Button
android:id="@+id/button_6"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="match_parent"
android:text="6"
android:textSize="25sp"/>
<Button
android:id="@+id/button_addition"
android:layout_weight="0.25"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="+"
android:textSize="25sp"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="140dp"
android:orientation="horizontal">

<LinearLayout
android:layout_width="310dp"
android:layout_height="140dp"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">

<Button
android:id="@+id/button_1"
android:layout_width="103dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:gravity="center"
android:text="1"
android:textSize="25sp" />

<Button
android:id="@+id/button_2"
android:layout_width="103dp"
android:layout_height="70dp"
android:text="2"
android:textSize="25sp" />

<Button
android:id="@+id/button_3"
android:layout_width="103dp"
android:layout_height="70dp"
android:text="3"
android:textSize="25sp" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_mod"
android:text="%"
android:layout_width="103dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_height="70dp"
android:textSize="25sp" />
<Button
android:id="@+id/button_0"
android:text="0"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="103dp"
android:layout_height="70dp"
android:textSize="25sp" />

<Button
android:id="@+id/button_point"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="103dp"
android:layout_height="70dp"
android:text="."
android:textSize="40sp" />
</LinearLayout>
</LinearLayout>

<Button
android:id="@+id/button_equal"
android:layout_width="100dp"
android:layout_height="140dp"
android:background="@android:color/holo_orange_dark"
android:text="="
android:textSize="40sp" />
</LinearLayout>

</LinearLayout>
接下来是MainAcvitity
package com.example.win10.jisuanqi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

private String str="";
private Button num0;
private Button num1;
private Button num2;
private Button num3;
private Button num4;
private Button num5;
private Button num6;
private Button num7;
private Button num8;
private Button num9;
/**
* 运算符
*/
/**
* 结果
*/
private Button button_addition; // +
private Button button_reduce; // -
private Button button_multiplication; // *
private Button button_division; // /
private Button button_equal; // =
/**
* 其他
*/
private Button button_point; // .
private Button button_mod; // %
private Button button_delete; //Del
private Button button_AC; // 0

private EditText mResultText;
private TextView mTextview;
private Boolean iscount=false;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
initEvent();
}

private void initView() {
/**
* 数字
*/
num0 = (Button) findViewById(R.id.button_0);
num1 = (Button) findViewById(R.id.button_1);
num2 = (Button) findViewById(R.id.button_2);
num3 = (Button) findViewById(R.id.button_3);
num4 = (Button) findViewById(R.id.button_4);
num5 = (Button) findViewById(R.id.button_5);
num6 = (Button) findViewById(R.id.button_6);
num7 = (Button) findViewById(R.id.button_7);
num8 = (Button) findViewById(R.id.button_8);
num9 = (Button) findViewById(R.id.button_9);
/**
* 运算符
*/
button_addition = (Button) findViewById(R.id.button_addition);
button_reduce = (Button) findViewById(R.id.button_reduce);
button_multiplication = (Button) findViewById(R.id.button_multiplication);
button_division = (Button) findViewById(R.id.button_division);
button_equal = (Button) findViewById(R.id.button_equal);
/**
* 其他
*/
button_point = (Button) findViewById(R.id.button_point);
button_mod = (Button) findViewById(R.id.button_mod);
button_delete = (Button) findViewById(R.id.button_delete);
button_AC = (Button) findViewById(R.id.button_AC);
mResultText=(EditText) findViewById(R.id.edit_result);
mTextview=(TextView) findViewById(R.id.tex_result);
}
private void initEvent() {
num0.setOnClickListener(this);
num1.setOnClickListener(this);
num2.setOnClickListener(this);
num3.setOnClickListener(this);
num4.setOnClickListener(this);
num5.setOnClickListener(this);
num6.setOnClickListener(this);
num7.setOnClickListener(this);
num8.setOnClickListener(this);
num9.setOnClickListener(this);

button_addition.setOnClickListener(this);
button_reduce.setOnClickListener(this);
button_multiplication.setOnClickListener(this);
button_division.setOnClickListener(this);
button_equal.setOnClickListener(this);

button_point.setOnClickListener(this);
button_mod.setOnClickListener(this);
button_delete .setOnClickListener(this);
button_AC.setOnClickListener(this);
}

public void onClick(View v)
{
if(iscount)
{
mResultText.setText("");
iscount=false;
}
switch (v.getId()) {
case R.id.button_0:
str=mResultText.getText().toString()+"0";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
//mTextview.setText(str);
break;
case R.id.button_1:
str=mResultText.getText().toString()+"1";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_2:
str=mResultText.getText().toString()+"2";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_3:
str=mResultText.getText().toString()+"3";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_4:
str=mResultText.getText().toString()+"4";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_5:
str=mResultText.getText().toString()+"5";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_6:
str=mResultText.getText().toString()+"6";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_7:
str=mResultText.getText().toString()+"7";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_8:
str=mResultText.getText().toString()+"8";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_9:
str=mResultText.getText().toString()+"9";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_addition:
str=mResultText.getText().toString()+"+";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_reduce:
str=mResultText.getText().toString()+"-";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_multiplication:
str=mResultText.getText().toString()+"*";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_division:
str=mResultText.getText().toString()+"÷";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_point:
str=mResultText.getText().toString()+".";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_mod:
str=mResultText.getText().toString()+"%";
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
case R.id.button_AC:
str="";
mResultText.setText(str);
break;
case R.id.button_equal:
mResultText.setText(str);
Count(str);
break;
case R.id.button_delete:
if(str.length()>0){
str=str.substring(0,str.length()-1);
mResultText.setText(str);
mResultText.setSelection( mResultText.getText().length());
break;
}
default:
}

}
public void Count(String str)
{
//mTextview.setText(str);
// Log.v("test",str+" sd ass as ");
if(str.contains("÷") ||str.contains("*") ||str.contains("+") ||str.contains("-") ||str.contains("%"))
{
String param1;
String param2;
double d1;
double d2;
String result;
Boolean flag=false;
if(str.contains("-")&&str.indexOf("-")==0)
{
str=str.substring(1);
flag=true;
}
if(str.contains("÷"))
{
param1 = str.substring(0, str.indexOf("÷"));
param2 = str.substring(str.indexOf("÷") + 1);
d1=Double.valueOf(param1).doubleValue();
d2=Double.valueOf(param2).doubleValue();
result=Double.toString(d1/d2);
if(flag)
result="-"+result;
mTextview.setText(result);
iscount=true;
}
else if(str.contains("*"))
{
param1 = str.substring(0, str.indexOf("*"));
param2 = str.substring(str.indexOf("*") + 1);
d1=Double.valueOf(param1).doubleValue();
d2=Double.valueOf(param2).doubleValue();
result=Double.toString(d1*d2);
if(flag)
result="-"+result;
mTextview.setText(result);
iscount=true;
}
else if(str.contains("+"))
{
param1 = str.substring(0, str.indexOf("+"));
param2 = str.substring(str.indexOf("+") + 1);
d1=Double.valueOf(param1).doubleValue();
d2=Double.valueOf(param2).doubleValue();
if(flag)
d1=-1*d1;
result=Double.toString(d1+d2);
mTextview.setText(result);
iscount=true;
}
else if(str.contains("%"))
{
param1 = str.substring(0, str.indexOf("%"));
param2 = str.substring(str.indexOf("%") + 1);
d1=Double.valueOf(param1).doubleValue();
d2=Double.valueOf(param2).doubleValue();
result=Double.toString(d1%d2);
mTextview.setText(result);
iscount=true;
}
else
{
param1 = str.substring(0, str.indexOf("-"));
param2 = str.substring(str.indexOf("-") + 1);
d1=Double.valueOf(param1).doubleValue();
d2=Double.valueOf(param2).doubleValue();
if(flag)
d1=-1*d1;
result=Double.toString(d1-d2);
mTextview.setText(result);
iscount=true;
}
flag=false;
mResultText.setSelection( mResultText.getText().length());

}

}
}

原文地址:https://www.cnblogs.com/CY-947205926/p/7748201.html