1
我已經添加了一個退格按鈕,但是當我點擊該按鈕時工作正常,但是當我點擊任何按鈕時,它會顯示我以前刪除的所有數字。還請告訴我如何執行多種操作,例如(1x2+5=?)
。如何正確執行計算器擦除按鈕?
當我點擊按鈕清除(onClickClear)它的作品,但當我點擊另一個數字,然後會自動出現我刪除的最後所有數字。
public class MainActivity extends Activity {
Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonAdd, buttonSub, buttonMulti, buttonDivide, buttonEqual;
Button button;
EditText editText;
String display = "";
String result = "";
String str = "";
String currentOperator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button9 = (Button) findViewById(R.id.button9);
button8 = (Button) findViewById(R.id.button8);
button7 = (Button) findViewById(R.id.button7);
button6 = (Button) findViewById(R.id.button6);
button5 = (Button) findViewById(R.id.button5);
button4 = (Button) findViewById(R.id.button4);
button3 = (Button) findViewById(R.id.button3);
button2 = (Button) findViewById(R.id.button2);
button1 = (Button) findViewById(R.id.button1);
button = (Button) findViewById(R.id.button);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonSub = (Button) findViewById(R.id.buttonSub);
buttonMulti = (Button) findViewById(R.id.buttonMulti);
buttonDivide = (Button) findViewById(R.id.buttonDivide);
editText = (EditText) findViewById(R.id.editText);
//editText.setText(display);
}
public void displayScreen(){
editText.setText(display);
}
public void onClick(View v){
int id = v.getId();
if (result != ""){
display = "";
currentOperator = "";
result = "";
displayScreen();
}
switch (id){
case R.id.button0:
display += button0.getText();
displayScreen();
break;
case R.id.button1:
display += button1.getText();
displayScreen();
break;
case R.id.button2:
display += button2.getText();
displayScreen();
break;
case R.id.button3:
display += button3.getText();
displayScreen();
break;
case R.id.button4:
display += button4.getText();
displayScreen();
break;
case R.id.button5:
display += button5.getText();
displayScreen();
break;
case R.id.button6:
display += button6.getText();
displayScreen();
break;
case R.id.button7:
display += button7.getText();
displayScreen();
break;
case R.id.button8:
display += button8.getText();
displayScreen();
break;
case R.id.button9:
display += button9.getText();
displayScreen();
break;
default:
break;
}
}
public void onClickOperator(View v){
int id = v.getId();
if(result != "") {
display = result;
result = "";
}
switch (id){
case R.id.buttonAdd:
display += buttonAdd.getText();
currentOperator = buttonAdd.getText().toString();
displayScreen();
break;
case R.id.buttonSub:
display += buttonSub.getText();
currentOperator = buttonSub.getText().toString();
displayScreen();
break;
case R.id.buttonMulti:
display += buttonMulti.getText();
currentOperator = buttonMulti.getText().toString();
displayScreen();
break;
case R.id.buttonDivide:
display += buttonDivide.getText();
currentOperator = buttonDivide.getText().toString();
displayScreen();
break;
default:
break;
}
}
// this for delete the digits from the editExt.
//It works but when i clicked the then
//Any number button then it shows all deleted digits
public void onClickClear(View v){
str = editText.getText().toString();
if (str.length() > 0) {
str = str.substring(0, str.length()-1);
editText.setText(str);
}
}
private double calculate(String a, String b, String op){
switch (op){
case "+": return Double.valueOf(a) + Double.valueOf(b);
case "-": return Double.valueOf(a) - Double.valueOf(b);
case "x": return Double.valueOf(a) * Double.valueOf(b);
case "/": return Double.valueOf(a)/Double.valueOf(b);
default:
return -1;
}
}
public void onClickEqual(View v){
String[] operation = display.split(Pattern.quote(currentOperator));
if(operation.length < 2) return;
result = String.valueOf(calculate(operation[0],operation[1],currentOperator));
editText.setText(display + "\n" + String.valueOf(result));
}
public void onClear(View v){
editText.setText(null);
display = "";
currentOperator = "";
result = "";
displayScreen();
}
}