Android——计算器

时间:2022-09-19 14:27:15

layout文件:

 <?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="4"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.hanqi.testapp2.PractiseActivity5">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_columnSpan="4"
android:gravity="bottom|right"
android:id="@+id/tv_1"
android:textSize="50sp"
android:paddingRight="10dp"
android:textColor="#0F0"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="c"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_c"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="←"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_tui"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="÷"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_chu"
android:onClick="bt_chu_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="×"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_cheng"
android:onClick="bt_cheng_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="7"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:onClick="bt_onClick"
android:id="@+id/bt7"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="8"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:onClick="bt_onClick"
android:id="@+id/bt8"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="9"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:onClick="bt9_onClick"
android:id="@+id/bt9"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="-"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_jian"
android:onClick="bt_jian_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="4"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt4"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="5"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt5"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="6"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt6"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="+"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_jia"
android:onClick="bt_jia_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt1"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt2"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt3"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="="
android:layout_columnWeight="1"
android:layout_rowSpan="2"
android:layout_rowWeight="1"
android:textSize="25sp"
android:layout_gravity="fill"
android:id="@+id/bt_deng"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="0"
android:layout_columnWeight="1"
android:layout_columnSpan="2"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt0"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="."
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:textSize="25sp"
android:id="@+id/bt_dian"/>
</GridLayout>

java类:

 package com.hanqi.testapp2;

 import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class PractiseActivity5 extends AppCompatActivity { TextView tv_1;
Button bt9;
Button bt4;
Button bt5;
Button bt6;
Button bt_jia;
Button bt_jian;
Button bt1;
Button bt2;
Button bt3;
Button bt_cheng;
Button bt0;
Button bt_dian;
Button bt_chu;
Button bt_tui;
Button bt7;
Button bt8;
Button bt_deng;
Button bt_c; //s1存储数据
double s1 = 0;
//判断运算
int s2 = 0;
//承载连续运算数字
double s3 = 0;
//显示结果
double s4;
//运算完成后使用结果直接下次运算
int s5=0;
//确认小数点输入一次
int s6=0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise5);
tv_1 = (TextView)findViewById(R.id.tv_1);
bt0 = (Button)findViewById(R.id.bt0);
bt1 = (Button)findViewById(R.id.bt1);
bt2 = (Button)findViewById(R.id.bt2);
bt3 = (Button)findViewById(R.id.bt3);
bt4 = (Button)findViewById(R.id.bt4);
bt5 = (Button)findViewById(R.id.bt5);
bt6 = (Button)findViewById(R.id.bt6);
bt7 = (Button)findViewById(R.id.bt7);
bt8 = (Button)findViewById(R.id.bt8);
bt9 = (Button)findViewById(R.id.bt9);
bt_jia = (Button)findViewById(R.id.bt_jia);
bt_jian = (Button)findViewById(R.id.bt_jian);
bt_cheng = (Button)findViewById(R.id.bt_cheng);
bt_dian = (Button)findViewById(R.id.bt_dian);
bt_chu = (Button)findViewById(R.id.bt_chu);
bt_tui = (Button)findViewById(R.id.bt_tui);
bt_deng = (Button)findViewById(R.id.bt_deng);
bt_c = (Button)findViewById(R.id.bt_c);
bt_tui = (Button)findViewById(R.id.bt_tui); bt_onClick bt_l = new bt_onClick();
bt_dian_onClick bt_dian1 = new bt_dian_onClick();
bt_jia_onClick bt_jia1 = new bt_jia_onClick();
bt_jian_onClick bt_jian1 = new bt_jian_onClick();
bt_cheng_onClick bt_cheng1 = new bt_cheng_onClick();
bt_chu_onClick bt_chu1 = new bt_chu_onClick();
bt_deng_onClick bt_deng1 = new bt_deng_onClick();
bt_c_onClick bt_c1 = new bt_c_onClick();
bt_tui_onClick bt_tui1 = new bt_tui_onClick(); bt0.setOnClickListener(bt_l);
bt1.setOnClickListener(bt_l);
bt2.setOnClickListener(bt_l);
bt3.setOnClickListener(bt_l);
bt4.setOnClickListener(bt_l);
bt5.setOnClickListener(bt_l);
bt6.setOnClickListener(bt_l);
bt7.setOnClickListener(bt_l);
bt8.setOnClickListener(bt_l);
bt9.setOnClickListener(bt_l);
bt_dian.setOnClickListener(bt_dian1);
bt_jia.setOnClickListener(bt_jia1);
bt_jian.setOnClickListener(bt_jian1);
bt_chu.setOnClickListener(bt_chu1);
bt_cheng.setOnClickListener(bt_cheng1);
bt_deng.setOnClickListener(bt_deng1);
bt_c.setOnClickListener(bt_c1);
bt_tui.setOnClickListener(bt_tui1);
}
class bt_onClick implements View.OnClickListener
{
@Override
public void onClick(View v) {
if (s5 == 1) {
tv_1.setText(null);
s1 = 0;
s2 = 0;
s3 = 0;
s4 = 0;
s5 = 0;
}
String str = tv_1.getText().toString();
//转成按钮
Button bt = (Button)v;
//取得按钮上的文字
String str2 = bt.getText().toString();
str += str2;
tv_1.setText(str);
}
}
class bt_dian_onClick implements View.OnClickListener {
public void onClick(View v) {
if(s5==1)
{
tv_1.setText(null);
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
} if(s6==1)
{
return;
}
Button bt = (Button) v;
String str = tv_1.getText().toString(); if(str.length()==0||str.equals("0.")){
tv_1.setText("0.");
return;
} String str2 = bt.getText().toString();
str += str2;
tv_1.setText(str);
s6=1;
}
}
class bt_jia_onClick implements View.OnClickListener
{
@Override
public void onClick(View v) {
s5 = 0;
String str=tv_1.getText().toString();
if (str.length()==0)
{
return;
}
if (s1 == 0) {
s1 = Double.parseDouble(str); } else {
s3 = Double.parseDouble(str);
if (s2 == 0)
{
s1 = s3;
}
if (s2 == 1)
{
s1 += s3;
}
if (s2 == 2)
{
s1 -= s3;
}
if (s2 == 3)
{
s1 *= s3;
}
if (s2==4&&s3!=0)
{
s1 /= s3;
}
if (s2==4&&s3==0)
{
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
}
tv_1.setText(null);
s2 = 1;
s6=0;
}
}
class bt_jian_onClick implements View.OnClickListener
{
@Override
public void onClick(View v) {
s5 = 0;
String str=tv_1.getText().toString();
if (str.length()==0)
{
return;
}
if (s1 == 0) {
s1 = Double.parseDouble(str); } else {
s3 = Double.parseDouble(str);
if (s2 == 0)
{
s1 = s3;
}
if (s2 == 1)
{
s1 += s3;
}
if (s2 == 2)
{
s1 -= s3;
}
if (s2 == 3)
{
s1 *= s3;
}
if (s2==4&&s3!=0)
{
s1 /= s3;
}
if (s2==4&&s3==0)
{
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
}
tv_1.setText(null);
s2 = 2;
s6=0;
}
}
class bt_cheng_onClick implements View.OnClickListener
{
@Override
public void onClick(View v) {
s5 = 0;
String str=tv_1.getText().toString();
if (str.length()==0)
{
return;
}
if (s1 == 0) {
s1 = Double.parseDouble(str); } else {
s3 = Double.parseDouble(str);
if (s2 == 0)
{
s1 = s3;
}
if (s2 == 1)
{
s1 += s3;
}
if (s2 == 2)
{
s1 -= s3;
}
if (s2 == 3)
{
s1 *= s3;
}
if (s2==4&&s3!=0)
{
s1 /= s3;
}
if (s2==4&&s3==0)
{
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
}
tv_1.setText(null);
s2 = 3;
s6=0;
}
}
class bt_chu_onClick implements View.OnClickListener
{
@Override
public void onClick(View v) {
s5 = 0;
String str=tv_1.getText().toString();
if (str.length()==0)
{
return;
}
if (s1 == 0) {
s1 = Double.parseDouble(str); } else {
s3 = Double.parseDouble(str);
if (s2 == 0)
{
s1 = s3;
}
if (s2 == 1)
{
s1 += s3;
}
if (s2 == 2)
{
s1 -= s3;
}
if (s2 == 3)
{
s1 *= s3;
}
if (s2==4&&s3!=0)
{
s1 /= s3;
}
if (s2==4&&s3==0)
{
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
}
tv_1.setText(null);
s2 = 4;
s6=0;
}
}
class bt_deng_onClick implements View.OnClickListener {
public void onClick(View v) {
String str = tv_1.getText().toString();
if(str.length()==0)
{
return;
}
s3 = Double.parseDouble(str);
if(s3==0&&s2==4)
{
return;
}
if(s2==0)
{
s4=s3;
}
if(s2==1)
{
s4=s1+s3;
}
if(s2==2)
{
s4=s1-s3;
}
if(s2==3)
{
s4=s1*s3;
}
if(s2==4&&s3!=0)
{
s4=s1/s3;
}
if (s2==4&&s3==0)
{
s1=0;
s2=0;
s3=0;
s4=0;
s5=0;
}
String str2=s4+"";
tv_1.setText(str2);
s5=1;
s2=0;
s6=0;
}
}
class bt_c_onClick implements View.OnClickListener {
public void onClick(View v) {
s1=0;
s2=0;
s3=0;
s5=0;
s4=0;
s6=0;
tv_1.setText(null);
}
}
class bt_tui_onClick implements View.OnClickListener {
public void onClick(View v) { String str = tv_1.getText().toString();
if (str.length() == 0) {
return;
}
String str2 = str.substring(0, str.length() - 1);
tv_1.setText(str2);
if (s5 == 1) {
s5 = 0;
}
}
}
}

可实现计算器的基本运算功能

感谢@storm47给予我的帮助