Android 中如何做一个简单的计算器?如何使用按钮和单个编辑文本来做到这一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10280263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do a simple calculator in Android? How to do it using Buttons and single edit text?
提问by zyonneo
I am learning Android so I want to keep things simple.
我正在学习 Android,所以我想让事情变得简单。
I have to add two numbers and display the result in a single editbox. To add with the second number you have store the first number in a variable. Then later add using this variable with the second number in the edit text.
我必须添加两个数字并将结果显示在一个编辑框中。要与第二个数字相加,您必须将第一个数字存储在一个变量中。然后稍后使用此变量添加编辑文本中的第二个数字。
If you look at the xml and Java Code below you will have a better idea. Thanks in advance.
如果您查看下面的 xml 和 Java 代码,您将有一个更好的主意。提前致谢。
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_gravity="bottom">
<Button android:text="1" android:padding="20px" android:layout_marginTop="38dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button1" android:layout_alignParentLeft="true"></Button>
<Button android:text="2" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button1" android:id="@+id/button2" android:layout_toRightOf="@+id/button1"></Button>
<Button android:text="3" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button2" android:id="@+id/button3" android:layout_toRightOf="@+id/button2"></Button>
<Button android:text="4" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button3" android:id="@+id/button4" android:layout_toRightOf="@+id/button3"></Button>
<Button android:text="5" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button5" android:layout_below="@+id/button1" android:layout_alignParentLeft="true"></Button>
<Button android:text="6" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button5" android:layout_alignLeft="@+id/button2" android:id="@+id/button6"></Button>
<Button android:text="7" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/button6" android:layout_alignLeft="@+id/button3" android:id="@+id/button7"></Button>
<Button android:text="8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="@+id/button4" android:id="@+id/button8" android:layout_below="@+id/button4"></Button>
<Button android:text="=" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="@+id/button11" android:id="@+id/button15" android:layout_below="@+id/button12"></Button>
<Button android:text="clear" android:layout_alignRight="@+id/button8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignBottom="@+id/button15" android:layout_alignLeft="@+id/button12" android:id="@+id/button16" android:layout_below="@+id/button12"></Button>
<Button android:text="-" android:layout_alignRight="@+id/button8" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button12" android:layout_below="@+id/button8"></Button>
<Button android:text="+" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button15" android:layout_toLeftOf="@+id/button16" android:id="@+id/button11"></Button>
<Button android:text="/" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_toLeftOf="@+id/button7" android:layout_alignTop="@+id/button15" android:id="@+id/button14"></Button>
<Button android:text="0" android:layout_alignRight="@+id/button14" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button14" android:id="@+id/button0"></Button>
<Button android:text="*" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_toLeftOf="@+id/button10" android:layout_alignTop="@+id/button14" android:id="@+id/button13"></Button>
<Button android:text="9" android:padding="20px" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="@+id/button13" android:id="@+id/button9" android:layout_alignParentLeft="true"></Button>
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_below="@+id/button14"></EditText>
</RelativeLayout>
</LinearLayout>
Java code
Java代码
package calci.tor;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class CalculatorActivity extends Activity {
/** Called when the activity is first created. */
public EditText display;
TextView edt;
Integer c;
String a="",b;
//int d=0;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt=(EditText)findViewById(R.id.editText1);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
b5=(Button)findViewById(R.id.button5);
b6=(Button)findViewById(R.id.button6);
b7=(Button)findViewById(R.id.button7);
b8=(Button)findViewById(R.id.button8);
b9=(Button)findViewById(R.id.button9);
b10=(Button)findViewById(R.id.button0);
b11=(Button)findViewById(R.id.button11);
b12=(Button)findViewById(R.id.button12);
b13=(Button)findViewById(R.id.button13);
b14=(Button)findViewById(R.id.button14);
b15=(Button)findViewById(R.id.button15);
b16=(Button)findViewById(R.id.button16);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "1");
}
});
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "2");
}
});
b3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
a=edt.getText().toString();
edt.setText(a+ "3");
}
});
b4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
a=edt.getText().toString();
edt.setText(a+ "4");
}
});
b5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
a=edt.getText().toString();
edt.setText(a+ "5");
}
});
b6.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "6");
}
});
b7.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
a=edt.getText().toString();
edt.setText(a+ "7");
}
});
b8.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "8");
}
});
b9.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "9");
}
});
b10.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "0");
}
});
b11.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
String aa;
aa=a; //To Store the first number displayed in edit text to aa
edt.setText("+");
edt.setText("");
//d=1;
c=Integer.parseInt(aa)+Integer.parseInt(a);
}
});
b12.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
edt.setText("-");
}
});
b13.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
edt.setText("*");
}
});
b14.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
edt.setText("/");
}
});
b15.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
//int c=Integer.parseInt(a)+Integer.parseInt(b);
//edt.setText(c);
display.setText(c);
}
});
b16.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
edt.setText("");
}
});
}
}
回答by zapl
If your calculator has no (
/ )
and you don't allow entering negative numbers then a simple algorithm to calculate a constant input of numbers and operations could work like this:
如果您的计算器没有(
/)
并且您不允许输入负数,那么计算恒定输入数字和运算的简单算法可以像这样工作:
You have one variable which represents the result (that you probably want to display each step in the EditText) and one to buffer unfinished results
您有一个代表结果的变量(您可能希望在 EditText 中显示每个步骤)和一个用于缓冲未完成的结果
if the operation is *
or /
:
apply it to the buffer
如果操作是*
或/
:将其应用于缓冲区
if the operation is +
or -
:
add buffer to result, overwrite buffer with new input
如果操作是+
或-
:向结果添加缓冲区,用新输入覆盖缓冲区
2 * 4 + 3 + 5 * 2 * 3 - 1 + 2
2 * 4 + 3 + 5 * 2 * 3 - 1 + 2
initially final result = 0, buffer = 1
2
-> result = 0, buffer = 2 (careful here, I interpreted the initial step as*2
)* 4
-> result = 0, buffer = (2*4) = 8+ 3
-> result = (0+8) = 8, buffer = 3+ 5
-> result = (8+3) = 11, buffer = 5* 2
-> result = 11, buffer = (5*2) = 10* 3
-> result = 11, buffer = (10*3) = 30- 1
-> result = (11+30) = 41, buffer = -1+ 2
-> result = (41-1) = 40, buffer = 2
最初最终结果 = 0,缓冲区 = 1
2
-> 结果 = 0,缓冲区 = 2(这里小心,我将初始步骤解释为*2
)* 4
-> 结果 = 0,缓冲区 = (2*4) = 8+ 3
-> 结果 = (0+8) = 8,缓冲区 = 3+ 5
-> 结果 = (8+3) = 11,缓冲区 = 5* 2
-> 结果 = 11,缓冲区 = (5*2) = 10* 3
-> 结果 = 11,缓冲区 = (10*3) = 30- 1
-> 结果 = (11+30) = 41,缓冲区 = -1+ 2
-> 结果 = (41-1) = 40,缓冲区 = 2
-> finally (e.g. when pressing =
) add both together: Answer = 42
-> 最后(例如按下时=
)将两者相加:答案 = 42
回答by BVR
//Below having correct programming .
//I did some changes in your code.This code was working nicely try this.
//`import android.os.Bundle;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public EditText display;
TextView edt;
Integer c,d,r,b;
String a="0",aa;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt=(EditText)findViewById(R.id.editText1);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
b5=(Button)findViewById(R.id.button5);
b6=(Button)findViewById(R.id.button6);
b7=(Button)findViewById(R.id.button7);
b8=(Button)findViewById(R.id.button8);
b9=(Button)findViewById(R.id.button9);
b10=(Button)findViewById(R.id.button0);
b11=(Button)findViewById(R.id.button11);
b12=(Button)findViewById(R.id.button12);
b13=(Button)findViewById(R.id.button13);
b14=(Button)findViewById(R.id.button14);
b15=(Button)findViewById(R.id.button15);
b16=(Button)findViewById(R.id.button16);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
a=a+"1";
edt.setText(a);
}
});
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
a=a+"2";
edt.setText(a);
}
});
b3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
a=edt.getText().toString();
a=a+"3";
edt.setText(a);
}
});
b4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
a=edt.getText().toString();
a=a+"4";
edt.setText(a);
}
});
b5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
a=edt.getText().toString();
a=a+"5";
edt.setText(a);
}
});
b6.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
a=a+"6";
edt.setText(a);
}
});
b7.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
a=edt.getText().toString();
a=a+"7";
edt.setText(a);
}
});
b8.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
a=a+"8";
edt.setText(a);
}
});
b9.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
a=a+"9";
edt.setText(a);
}
});
b10.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
a=a+"0";
edt.setText(a);
}
});
b11.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
aa=a;
b=1;
a="";
edt.setText("+");
edt.setText("");
}
});
b12.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
aa=a;
b=2;
a="";
edt.setText("-");
edt.setText("");
}
});
b13.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
aa=a;
b=3;
a="";
edt.setText("*");
edt.setText("");
}
});
b14.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
aa=a;
b=4;
a="";
edt.setText("/");
edt.setText("");
}
});
b15.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
if(b==1){
c=Integer.parseInt(aa);
d=Integer.parseInt(a);
r=c+d;
}
else
if(b==4){
c=Integer.parseInt(aa);
d=Integer.parseInt(a);
r=c/d;
}else
if(b==2){
c=Integer.parseInt(aa);
d=Integer.parseInt(a);
r=c-d;
}
else
if(b==3){
c=Integer.parseInt(aa);
d=Integer.parseInt(a);
r=c*d;
}
Toast.makeText(MainActivity.this, "Result is::"+r, 10000).show();
c=0;
b=0;
d=0;
a="";
aa="";
edt.setText("");
}
});
b16.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
edt.setText("");
}
});
}`
回答by BVR
Your above code was almost correct but small logic was missed in that programming . After clicking button it will store string values into "a" its ok . After that we click on operator symbol "a" will store into "aa" variable.
您上面的代码几乎是正确的,但是在该编程中遗漏了一些小逻辑。单击按钮后,它将字符串值存储到 "a" 中。之后我们点击运算符符号“a”将存储到“aa”变量中。
example:
例子:
a="4";
aa=a;//aa="4"
//After you do like this below way .
c=Integer.parseInt(aa)+Integer.parseInt(a);
//In the above line c (storing integer values) = 4+4;
//a having "4" and aa="4".