基本计算器Android应用

时间:2016-06-11 14:18:52

标签: android string textview

我正在开发一个BASIC CALCULATOR android应用程序,我坚持到某个阶段。此应用程序从textview获取输入并将其存储在数字中。

从用户那里获取第二个输入时出现问题。

我将用于获取第一个输入(第一个数字)的字符串设置为null。使用相同的字符串,如何在定义操作后使字符串为null并允许第二个输入。那里的任何人都可以帮助我并感激我。

提前谢谢..

public class MainActivity extends AppCompatActivity {

    String answer = "";
    float num1, num2;
    int flag = 0;
    String operation = "";
    TextView textViewAnswer;
    Button buttonOne;
    Button buttonTwo;
    Button buttonThree;
    Button buttonFour;
    Button buttonFive;
    Button buttonSix;
    Button buttonSeven;
    Button buttonEight;
    Button buttonNine;
    Button buttonZero;
    Button buttonAddition;
    Button buttonSubstraction;
    Button buttonMultiplication;
    Button buttonDivision;
    Button buttonDot;
    Button buttonEqual;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewAnswer = (TextView) findViewById(R.id.textview_answer);
    }


    public void one(View view){
        buttonOne = (Button) findViewById(R.id.button_one);

        if (flag == 1) {
            num1 = Float.parseFloat(textViewAnswer.getText().toString());
            flag = 0;
            answer = "";
        }
        else {
            checkLength(buttonOne);
            textViewAnswer.setText(answer);
        }
    }

    public void two(View view){
        buttonTwo = (Button) findViewById(R.id.button_two);
        checkLength(buttonTwo);
        textViewAnswer.setText(answer);
        if (flag == 1) {
            num1 = Float.parseFloat(textViewAnswer.getText().toString());
            flag = 0;
            answer = "";
        }


    }

    public void three(View view){
        buttonThree = (Button) findViewById(R.id.button_three);
        checkLength(buttonThree);
        textViewAnswer.setText(answer);
    }

    public void four(View view){
        buttonFour = (Button) findViewById(R.id.button_four);
        checkLength(buttonFour);
        textViewAnswer.setText(answer);
    }

    public void five(View view){
        buttonFive = (Button) findViewById(R.id.button_five);
        checkLength(buttonFive);
        textViewAnswer.setText(answer);
    }

    public void six(View view){
        buttonSix = (Button) findViewById(R.id.button_six);
        checkLength(buttonSix);
        textViewAnswer.setText(answer);
    }

    public void seven(View view){
        buttonSeven = (Button) findViewById(R.id.button_seven);
        checkLength(buttonSeven);
        textViewAnswer.setText(answer);
    }

    public void eight(View view){
        buttonEight = (Button) findViewById(R.id.button_eight);
        checkLength(buttonEight);
        textViewAnswer.setText(answer);
    }

    public void nine(View view){
        buttonNine = (Button) findViewById(R.id.button_nine);
        checkLength(buttonNine);
        textViewAnswer.setText(answer);
    }

    public void zero(View view){
        buttonZero = (Button) findViewById(R.id.button_zero);
        checkLength(buttonZero);
        textViewAnswer.setText(answer);
    }

    public void dot(View view){
        buttonDot = (Button) findViewById(R.id.button_dot);
        checkLength(buttonDot);
        textViewAnswer.setText(answer);
    }

    public void equal(View view){
        buttonEqual = (Button) findViewById(R.id.button_equal);
//        textViewAnswer.setText(buttonEqual.getText().toString());
        switch (operation){
            case "+" : num2 = num1 + (Float.parseFloat(textViewAnswer.getText().toString()));
                        answer = ( String.valueOf(num2) );
                textViewAnswer.setText(answer);
        }
    }

    public void addition(View view){
        buttonAddition = (Button) findViewById(R.id.button_addition);
        textViewAnswer.setText(buttonAddition.getText().toString());
        operation = "+";
        flag = 1;

    }

    public void substraction(View view){
        buttonSubstraction = (Button) findViewById(R.id.button_substraction);
        textViewAnswer.setText(buttonSubstraction.getText().toString());
        operation = "-";
    }

    public void division(View view){
        buttonDivision = (Button) findViewById(R.id.button_division);
        textViewAnswer.setText(buttonDivision.getText().toString());
        operation = "/";
    }

    public void multiplication(View view){
        buttonMultiplication = (Button) findViewById(R.id.button_multiplication);
        textViewAnswer.setText(buttonMultiplication.getText().toString());
        operation = "*";
    }

    public void checkLength(Button button){
        if((answer.length()) < 9)
            answer += button.getText().toString();
        else
            Toast.makeText(this, "Cannot take more input", Toast.LENGTH_SHORT).show();
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.darpan.basiccalculator.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="bottom">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Answer"
            android:id="@+id/textview_answer"
            android:textSize="50sp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1"
                android:onClick="one"/>

            <Button
                android:id="@+id/button_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2"
                android:onClick="two"/>

            <Button
                android:id="@+id/button_three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3"
                android:onClick="three"/>

            <Button
                android:id="@+id/button_addition"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="+"
                android:onClick="addition"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button_four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4"
                android:onClick="four"/>

            <Button
                android:id="@+id/button_five"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="5"
                android:onClick="five"/>

            <Button
                android:id="@+id/button_six"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="6"
                android:onClick="six"/>

            <Button
                android:id="@+id/button_substraction"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="-"
                android:onClick="substraction"/>


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button

                android:id="@+id/button_seven"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="7"
                android:onClick="seven"/>

            <Button
                android:id="@+id/button_eight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="8"
                android:onClick="eight"/>

            <Button
                android:id="@+id/button_nine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="9"
                android:onClick="nine"/>
            />

            <Button
                android:id="@+id/button_multiplication"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="*"
                android:onClick="multiplication"/>
            />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button_dot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="."
                android:onClick="dot"/>

            <Button
                android:id="@+id/button_zero"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="0"
                android:onClick="zero"/>

            <Button
                android:id="@+id/button_equal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="="
                android:onClick="equal"/>

            <Button
                android:id="@+id/button_division"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="/"
                android:onClick="division"/>
        </LinearLayout>

    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:1)

您只需要重置TextView,而不是变量。另外,使用onCreate方法设置屏幕。我假设您在xml布局文件中声明了按钮方法,因此为了方便起见,将它们全部设置为使用相同的方法。

int operation; //1 = add, 2 = subt, 3 = div, 4 = mult
float answer;
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textViewAnswer = (TextView) findViewById(R.id.textview_answer);

    SetupView();
}


public void ButtonPress(View view) {

    switch (view.getId()){

    case R.id.button_one:
        textViewAnswer.setText(s + "1");
            s = textViewAnswer.getText();
    break;

    case R.id.button_two:
        textViewAnswer.setText(s + "2");            
            s = textViewAnswer.getText();
    break;
    .
    .
    .

    case R.id.button_addition:
        num1 = Float.parseFloat(textViewAnswer);
        //insert data persistence here one day (`SharedPreferences`)
        operation = 1;
        textViewAnswer.setText("");
        break;    
    .
    .
    .

    case R.id.button_equals:
        num2 = Float.parseFloat(textViewAnswer);

        switch (operation){

        case 1:
        answer = num1 + num2;
        textViewAnswer.setText(String.valueOf(answer))
        break;
        .
        .
        .
    }
}

Public void SetupView() {

buttonOne = (Button) findViewById(R.id.button_one);
buttonTwo = (Button) findViewById(R.id.button_two);
... etc
}