如何使用括号在textview中显示完整的计算?例如:2 + 3 - ((4/2)* 9) 我正在制作计算器应用程序,它使用括号并记住计算历史记录,并应在textview中显示所有数学运算。
这是我的代码:
package com.example.calculater;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView textdisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textdisplay = (TextView) findViewById(R.id.editText1);
Button b1 = (Button) findViewById(R.id.btn1);
Button b2 = (Button) findViewById(R.id.btn2);
Button b3 = (Button) findViewById(R.id.btn3);
Button b4 = (Button) findViewById(R.id.btn4);
Button b5 = (Button) findViewById(R.id.btn5);
Button b6 = (Button) findViewById(R.id.btn6);
Button b7 = (Button) findViewById(R.id.btn7);
Button b8 = (Button) findViewById(R.id.btn8);
Button b9 = (Button) findViewById(R.id.btn9);
Button b0 = (Button) findViewById(R.id.btn0);
Button multiply1 = (Button) findViewById(R.id.multiply);
Button divide1 = (Button) findViewById(R.id.divide);
Button plus1 = (Button) findViewById(R.id.plus);
Button minus1 = (Button) findViewById(R.id.minus);
Button equal1 = (Button) findViewById(R.id.equal);
Button clear1 = (Button) findViewById(R.id.clear);
Button back1 = (Button) findViewById(R.id.backspace);
Button dot1 = (Button) findViewById(R.id.decimal);
Button plusminus1 = (Button) findViewById(R.id.plusminus);
Button history1= (Button) findViewById(R.id.history);
Button open1 = (Button) findViewById(R.id.open);
Button close1 = (Button) findViewById(R.id.close);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b0.setOnClickListener(this);
multiply1.setOnClickListener(this);
divide1.setOnClickListener(this);
plus1.setOnClickListener(this);
minus1.setOnClickListener(this);
equal1.setOnClickListener(this);
clear1.setOnClickListener(this);
back1.setOnClickListener(this);
dot1.setOnClickListener(this);
plusminus1.setOnClickListener(this);
history1.setOnClickListener(this);
open1.setOnClickListener(this);
close1.setOnClickListener(this);
}
@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;
}
int clear_flag = 0;
String sign_flag = "";
Double total = 0.0;
int last_button = 0;
public void shownum (String number){
if(clear_flag==1){
textdisplay.setText("");
clear_flag=0;
}
else if(textdisplay.getText()=="0"){
textdisplay.setText("");
}
textdisplay.setText(textdisplay.getText() + number);
}
public void showsign(String sign){
if(last_button==R.id.plus || last_button==R.id.minus || last_button==R.id.multiply
|| last_button==R.id.divide){
}
else{
clear_flag = 1;//set flag
Double newNumber = Double.parseDouble(textdisplay.getText().toString());
if(sign_flag == "" || sign_flag == "="){
total = newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "+"){
total = total + newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "-"){
total = total - newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "*"){
total = total*newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "/"){
total = total/newNumber;
textdisplay.setText(total.toString());
}}
sign_flag = sign;
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn0){
shownum ("0");
}
else if(v.getId() == R.id.btn1){
shownum ("1");
}
else if(v.getId() == R.id.btn2){
shownum ("2");
}
else if(v.getId() == R.id.btn3){
shownum ("3");
}
else if(v.getId() == R.id.btn4){
shownum ("4");
}
else if(v.getId() == R.id.btn5){
shownum ("5");
}
else if(v.getId() == R.id.btn6){
shownum ("6");
}
else if(v.getId() == R.id.btn7){
shownum ("7");
}
else if(v.getId() == R.id.btn8){
shownum ("8");
}
else if(v.getId() == R.id.btn9){
shownum ("9");
}
else if(v.getId() == R.id.clear){
textdisplay.setText("");////ORIGINALLY ITS WAS 0 ""
total = 0.0;
sign_flag = "";
}
else if(v.getId() == R.id.decimal){
if(clear_flag==1){
textdisplay.setText("");
clear_flag = 0;
}
if(textdisplay.getText().toString().indexOf(".")<0){
textdisplay.setText(textdisplay.getText() + ".");
}}
else if(v.getId() == R.id.backspace){
if(textdisplay.getText().toString().length()>0){
int start = 0;
int end = textdisplay.getText().toString().length()-1;
String newText = textdisplay.getText().toString().substring(start,end);
textdisplay.setText(newText);
}}
else if(v.getId() == R.id.plus){
showsign("+");
}
else if(v.getId() == R.id.minus){
showsign("-");
}
else if(v.getId() == R.id.multiply){
showsign("*");
}
else if(v.getId() == R.id.divide){
showsign("/");
}
else if(v.getId() == R.id.equal){
Double newNumber = Double.parseDouble(textdisplay.getText().toString());
if(sign_flag == "+"){
total = total+newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "-"){
total = total-newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "*"){
total = total*newNumber;
textdisplay.setText(total.toString());
}
else if(sign_flag == "/"){
total = total/newNumber;
textdisplay.setText(total.toString());
}
sign_flag = "=";
} //when minus is pressed before any number input, applications closes.
else if (v.getId() == R.id.plusminus){
String number = textdisplay.getText().toString();
if(number == null) return; //exits function
if(number.equals("")) return; //exits function
// textdisplay.setText("Please enter number first then press +/-");
Double newNumber = Double.parseDouble(number);
total = newNumber * (-1);
textdisplay.setText(total.toString());
}
else if (v.getId()== R.id.open){
}
else if (v.getId()== R.id.close){
}
last_button = v.getId();
}}
Here is my XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5pt"
android:layout_weight="0.58"
android:inputType="numberDecimal" >
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" />
<Button
android:id="@+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
<Button
android:id="@+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
<Button
android:id="@+id/backspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
<Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="@+id/plusminus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+/-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="@+id/decimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="." />
<Button
android:id="@+id/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="( " />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.58" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.20" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.04"
android:orientation="vertical" >
<Button
android:id="@+id/history"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:text="History" />
<Button
android:id="@+id/equal"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:text="@string/_" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
codeMagic说的是,将每个存储在一个变量中,然后可以创建一个final String
变量来保存String.format
。也许
String finalString = String.format("This is the result %s and %s" ,
variableName, variableName);
然后
textdisplay.setText(finalString);