android studio计算器代码
时间: 2023-06-05 21:47:23 浏览: 495
android studio计算器实现代码
下面是一份简单的 Android Studio 计算器代码,它可以实现加减乘除运算:
```java
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnAdd, btnSubtract, btnMultiply, btnDivide;
EditText etFirstNumber, etSecondNumber;
TextView tvResult;
float result;
float firstNumber, secondNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnSubtract = (Button) findViewById(R.id.btnSubtract);
btnMultiply = (Button) findViewById(R.id.btnMultiply);
btnDivide = (Button) findViewById(R.id.btnDivide);
etFirstNumber = (EditText) findViewById(R.id.etFirstNumber);
etSecondNumber = (EditText) findViewById(R.id.etSecondNumber);
tvResult = (TextView) findViewById(R.id.tvResult);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstNumber = Float.parseFloat(etFirstNumber.getText().toString());
secondNumber = Float.parseFloat(etSecondNumber.getText().toString());
result = firstNumber + secondNumber;
tvResult.setText(Float.toString(result));
}
});
btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstNumber = Float.parseFloat(etFirstNumber.getText().toString());
secondNumber = Float.parseFloat(etSecondNumber.getText().toString());
result = firstNumber - secondNumber;
tvResult.setText(Float.toString(result));
}
});
btnMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstNumber = Float.parseFloat(etFirstNumber.getText().toString());
secondNumber = Float.parseFloat(etSecondNumber.getText().toString());
result = firstNumber * secondNumber;
tvResult.setText(Float.toString(result));
}
});
btnDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstNumber = Float.parseFloat(etFirst以下是一个简单的Android Studio计算器的代码示例,该示例只实现了加减乘除四种运算:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText input1, input2;
private TextView result;
private Button addBtn, subBtn, mulBtn, divBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input1 = findViewById(R.id.input1);
input2 = findViewById(R.id.input2);
result = findViewById(R.id.result);
addBtn = findViewById(R.id.addBtn);
subBtn = findViewById(R.id.subBtn);
mulBtn = findViewById(R.id.mulBtn);
divBtn = findViewById(R.id.divBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(input1.getText().toString());
double num2 = Double.parseDouble(input2.getText().toString());
double sum = num1 + num2;
result.setText(String.valueOf(sum));
}
});
subBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(input1.getText().toString());
double num2 = Double.parseDouble(input2.getText().toString());
double diff = num1 - num2;
result.setText(String.valueOf(diff));
}
});
mulBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(input1.getText().toString());
double num2 = Double.parseDouble(input2.getText().toString());
double mul = num1 * num2;
result.setText(String.valueOf(mul));
}
});
divBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double num1 = Double.parseDouble(input1.getText().toString());
double num2 = Double.parseDouble(input2.getText().toString());
if(num2 != 0) {
double div = num1 / num2;
result.这是一个简单的 Android Studio 计算器的示例代码:
```java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num1, num2;
Button add, sub, mul, div;
TextView result;
float result_num;
int num1_int, num2_int;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText)findViewById(R.id.num1);
num2 = (EditText)findViewById(R.id.num2);
add = (Button)findViewById(R.id.add);
sub = (Button)findViewById(R.id.sub);
mul = (Button)findViewById(R.id.mul);
div = (Button)findViewById(R.id.div);
result = (TextView)findViewById(R.id.result);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1_int = Integer.parseInt(num1.getText().toString());
num2_int = Integer.parseInt(num2.getText().toString());
result_num = num1_int + num2_int;
result.setText(String.valueOf(result_num));
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1_int = Integer.parseInt(num1.getText().toString());
num2_int = Integer.parseInt(num2.getText().toString());
result_num = num1_int - num2_int;
result.setText(String.valueOf(result_num));
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1_int = Integer.parseInt(num1.getText().toString());
num2_int = Integer.parseInt(num2.getText().toString());
result_num = num1_int * num2_int;
result.setText(String.valueOf(result_num));
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
num1_int = Integer.parseInt(num1.getText().toString());
num2_int = Integer.parseInt(num2.getText().toString());
result_num = (float)num1_int / (float)num2_int;
result.setText这是一段简单的Android Studio计算器代码示例:
```java
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText number1;
EditText number2;
Button add;
Button subtract;
Button multiply;
Button divide;
TextView result;
float result_num;
int num1, num2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1 = (EditText) findViewById(R.id.number1);
number2 = (EditText) findViewById(R.id.number2);
add = (Button) findViewById(R.id.add);
subtract = (Button) findViewById(R.id.subtract);
multiply = (Button) findViewById(R.id.multiply);
divide = (Button) findViewById(R.id.divide);
result = (TextView) findViewById(R.id.result);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 + num2;
result.setText(String.valueOf(result_num));
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 - num2;
result.setText(String.valueOf(result_num));
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = num1 * num2;
result.setText(String.valueOf(result_num));
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Integer.parseInt(number1.getText().toString());
num2 = Integer.parseInt(number2.getText().toString());
result_num = (float)num1 / num2;
result.setText(String.valueOf(result_num));这是一个简单的Android Studio计算器代码示例:
```
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add;
Button subtract;
Button multiply;
Button divide;
TextView result;
float result_num;
int num1_int, num2_int;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = (EditText)findViewById(R.id.num1);
num2 = (EditText)findViewById(R.id.num2);
add = (Button)findViewById(R.id.add);
subtract = (Button)findViewById(R.id.subtract);
multiply = (Button)findViewById(R.id.multiply);
divide = (Button)findViewById(R.id.divide);
result = (TextView)findViewById(R.id.result);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1_int = Integer.parseInt(num1.getText().toString());
num2_int = Integer.parseInt(num2.getText().toString());
result_num = num1_int + num2_int;
result.setText(String.valueOf(result_num));
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1_int = Integer.parseInt(num1.getText().toString());
num2_int = Integer.parseInt(num2.getText().toString());
result_num = num1_int - num2_int;
result.setText(String.valueOf(result_num));
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1_int = Integer.parseInt(num1.getText().toString());
num2_int = Integer.parseInt(num2.getText().toString());
result_num = num1_int * num2_int;
result.setText(String.valueOf(result_num));
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1_int = Integer.parseInt(num1.getText().toString());
num2_int = Integer.parseInt(num2.getText().toString());
result_num = (float)num1_int /上面给出的代码是一个简单的Android Studio计算器的代码示例,该示例只实现了加减乘除四种运算。在这个示例中,定义了4个按钮(加、减、乘、除)、2个输入框(输入数字1和数字2)和1个文本框(显示运算结果),并为每个按钮添加点击事件,在点击事件中实现对应的运算。请注意,这只是一个简单的示例,可能需要根据您的需求进行修改。
阅读全文