我制作了一个安卓计算器。虽然它不完全是一个普通的计算器。在普通的计算器中,当我们加起来两个数字时,假设5 + 5,我们点击其中的任何一个,/ * - + =,数字总和,并在结果上实现新的算术逻辑。虽然它对我不起作用。有人可以告诉我我的代码中应该做出哪些更改? 谢谢。 :)
package com.example.asd;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.Window;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Typeface font1, font2;
TextView tv1;
private EditText Scr; //textbox screen
private float NumberBf; //Save screen before pressing button operation;
private String Operation;
private ButtonClickListener btnClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
font1=Typeface.createFromAsset(getAssets(), "digits.ttf");
Scr=(EditText)findViewById(R.id.editText);
Scr.setTypeface(font1);
font2=Typeface.createFromAsset(getAssets(), "alexbrush.TTF");
tv1=(TextView)findViewById(R.id.textView1);
tv1.setTypeface(font2);
Scr = (EditText) findViewById(R.id.editText);
Scr.setEnabled(false);
btnClick = new ButtonClickListener();
int idList[] = {R.id.button0,R.id.button7, R.id.button1, R.id.button8,R.id.button9,R.id.button4,
R.id.button5,R.id.button6,R.id.button,R.id.button2,R.id.button3,R.id.buttonDot,
R.id.buttonMul,R.id.buttonDiv,R.id.buttonAdd,R.id.buttonSub,R.id.buttonC,
R.id.buttonEq, R.id.buttonSqrt, R.id.buttonsquare, R.id.buttonNp
};
for(int id:idList){
View v = (View) findViewById(id);
v.setOnClickListener(btnClick);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void mMath(String str){
NumberBf = Float.parseFloat(Scr.getText().toString()); //save the screen
Operation = str; //save operation
Scr.setText("0"); //Clear screen
}
public void getKeyboard(String str){
String ScrCurrent = Scr.getText().toString();
if(ScrCurrent.equals("0"))
ScrCurrent = "";
ScrCurrent += str;
Scr.setText(ScrCurrent);
}
public void mResult(){
float NumAf = Float.parseFloat(Scr.getText().toString());
float result = 0;
if(Operation.equals("+")){
result = NumAf + NumberBf;
}
if(Operation.equals("-")){
result = NumberBf - NumAf;
}
if(Operation.equals("*")){
result = NumAf * NumberBf;
}
if(Operation.equals("/")){
result = NumberBf / NumAf;
}
Scr.setText(String.format("%10d", result));
}
public void fnSqrt(){
double Number = Double.parseDouble(Scr.getText().toString());
Number = Math.sqrt(Number);
Scr.setText(String.valueOf(Number));
}
public void fnSquare(){
float Number1 = Float.parseFloat(Scr.getText().toString());
Number1 = pow(Number1, 2);
Scr.setText(String.valueOf(Number1));
}
public void fnNp(){
float Number = Float.parseFloat(Scr.getText().toString());
Number = Number*(-1);
Scr.setText(String.valueOf(Number));
}
//new class ButtonClickListener
private float pow(float number1, int i) {
// TODO Auto-generated method stub
return number1*number1;
}
private class ButtonClickListener implements OnClickListener{
public void onClick(View v){
switch (v.getId()){
case R.id.buttonC: //Clear screen
Scr.setText("0");
NumberBf = 0;
Operation = "";
break;
case R.id.buttonAdd: //function Add
mMath("+");
break;
case R.id.buttonSub:
mMath("-");
break;
case R.id.buttonMul:
mMath("*");
break;
case R.id.buttonDiv:
mMath("/");
break;
case R.id.buttonEq:
mResult();
break;
case R.id.buttonSqrt:
fnSqrt();
break;
case R.id.buttonNp:
fnNp();
break;
case R.id.buttonsquare:
fnSquare();
break;
default:
String numb = ((Button) v).getText().toString();
getKeyboard(numb);
break;
}
}
}
}
答案 0 :(得分:0)
此代码有效。我过去做过一个解析计算器,看起来完全不同。 我建议使用一个调试器,使用bug按钮在eclipse中运行,当你遇到问题时,在事件处理方法中添加一个断点。并逐步使用跳过按钮,并进入任何可能有bug的方法。可能需要做几次。
我需要使用调试器来使下面的代码工作。因为它可以让你改变操作,例如按3 + - 2 =将给1; +/-按钮还没有工作,因为代码被简化以查看按钮的标签,特别是第一个字符。
我在调试器中发现的是:
还发现,必须重新测试整数除以整数,以确定结果是整数还是浮点数。不记得调试器是否为我找到了。
package com.example.calc1;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Number lastNumber;
char lastOp;
char beforeLastOp;
char pendingOp;
private boolean currentNumberDecimal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void buttonPressed(View view) {
Button b = (Button) view;
Log.d("Buttons ", "pressed " + b);
EditText edit1 = (EditText) findViewById(R.id.editText1);
char ch = b.getText().length() > 0 ? b.getText().charAt(0) : 0;
switch (ch) {
case '=':
doCalculation(edit1);
edit1.setText(lastNumber.toString());
lastOp = 0;
beforeLastOp = 0;
lastNumber = null;
break;
case '+':
case '-':
case '*':
case '/':
lastOp = ch;
break;
case 'A':
edit1.getText().clear();
lastNumber = null;
lastOp = 0;
break;
}
if ((ch == '.' || Character.isDigit(ch)) && lastOp != 0) {
doCalculation(edit1);
beforeLastOp = lastOp;
lastOp = 0;
edit1.getText().clear();
currentNumberDecimal = ch == '.';
}
if ((ch == '.' && !currentNumberDecimal) || Character.isDigit(ch)) {
edit1.getText().append(ch);
}
if (ch == '.' && !currentNumberDecimal)
currentNumberDecimal = true;
}
private void doCalculation(EditText edit1) {
String s = edit1.getText().toString();
float f = Float.parseFloat(s);
boolean sigDiff = true;
try {
float f2 = (float) Integer.parseInt(s);
sigDiff = Math.abs(f - f2) > Float.MIN_NORMAL;
} catch (NumberFormatException nfe) {
sigDiff = true;
}
if (lastNumber == null && !sigDiff) {
lastNumber = Integer.parseInt(s);
} else if (lastNumber == null && sigDiff) {
lastNumber = f;
} else if (!sigDiff && lastNumber.getClass().equals(Integer.class)) {
int i = Integer.parseInt(s);
switch (beforeLastOp) {
case '+':
lastNumber = lastNumber.intValue() + i;
break;
case '-':
lastNumber = lastNumber.intValue() - i;
break;
case '*':
lastNumber = lastNumber.intValue() * i;
break;
case '/':
Number n = lastNumber.intValue() / i;
float f3 = lastNumber.floatValue() / i;
if (Math.abs(f3 - n.floatValue()) > Float.MIN_NORMAL) {
lastNumber = f3;
} else {
lastNumber = n;
}
break;
default:
lastNumber = i;
break;
}
} else {
switch (beforeLastOp) {
case '+':
lastNumber = lastNumber.floatValue() + f;
break;
case '-':
lastNumber = lastNumber.floatValue() - f;
break;
case '*':
lastNumber = lastNumber.floatValue() * f;
break;
case '/':
lastNumber = lastNumber.floatValue() / f;
break;
default:
lastNumber = f;
break;
}
}
}
}
答案 1 :(得分:0)
查看您的代码,
怎么样?public void mMath(String str){
if (Operation != "" && Operation != null)
mResult();
NumberBf = Float.parseFloat(Scr.getText().toString()); //save the screen
Operation = str; //save operation
Scr.setText("0"); //Clear screen
}