Android Launcher显示计算器活动布局但不执行任何操作

时间:2013-10-09 12:01:55

标签: java android

您好我正在使用Android启动器来显示计算器活动。它显示布局很好,但当你点击任何我需要帮助的按钮时我什么也没做什么我已经尝试了所有的东西,我感到很沮丧!任何帮助都会很棒!

Calculator.java:

package com.dva.schooltoolshome;

import java.text.DecimalFormat;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Calculator extends FragmentActivity implements OnClickListener {

    private TextView calculatorDisplay;
    private static final String DIGITS = "0123456789.";
    private Boolean userIsInTheMiddleOfTypingANumber = false;

    DecimalFormat df = new DecimalFormat("@###########");

    CalculatorBrain brain;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // hide the status bar and other OS-level chrome
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        brain = new CalculatorBrain();

        calculatorDisplay = (TextView) findViewById(R.id.textView1);

        df.setMinimumFractionDigits(0);
        df.setMinimumIntegerDigits(1);
        df.setMaximumIntegerDigits(8);

        findViewById(R.id.button0).setOnClickListener(this);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        findViewById(R.id.button6).setOnClickListener(this);
        findViewById(R.id.button7).setOnClickListener(this);
        findViewById(R.id.button8).setOnClickListener(this);
        findViewById(R.id.button9).setOnClickListener(this);

        findViewById(R.id.buttonAdd).setOnClickListener(this);
        findViewById(R.id.buttonSubtract).setOnClickListener(this);
        findViewById(R.id.buttonMultiply).setOnClickListener(this);
        findViewById(R.id.buttonDivide).setOnClickListener(this);
        findViewById(R.id.buttonToggleSign).setOnClickListener(this);
        findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);
        findViewById(R.id.buttonEquals).setOnClickListener(this);
        findViewById(R.id.buttonClear).setOnClickListener(this);
        findViewById(R.id.buttonClearMemory).setOnClickListener(this);
        findViewById(R.id.buttonAddToMemory).setOnClickListener(this);
        findViewById(R.id.buttonSubtractFromMemory).setOnClickListener(this);
        findViewById(R.id.buttonRecallMemory).setOnClickListener(this);

        // The following buttons only exist in layout-land (Landscape mode) and require extra attention.
        // The messier option is to place the buttons in the regular layout too and set android:visibility="invisible". 
        if (findViewById(R.id.buttonSquareRoot) != null) {
            findViewById(R.id.buttonSquareRoot).setOnClickListener(this);
        }   
        if (findViewById(R.id.buttonInvert) != null) {
            findViewById(R.id.buttonInvert).setOnClickListener(this);
        }
        if (findViewById(R.id.buttonCos) != null) {
            findViewById(R.id.buttonCos).setOnClickListener(this);
        }   
        if (findViewById(R.id.buttonSin) != null) {
            findViewById(R.id.buttonSin).setOnClickListener(this);
        }

//      Another way to hide the window title and actionbar, but only in newer sdk's
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//            ActionBar actionBar = getActionBar();
//            actionBar.setDisplayShowHomeEnabled(false);
//            actionBar.setDisplayShowTitleEnabled(false);
//            actionBar.hide();
//        }
    }

    // @Override
    public void onClick(View view) {

        String buttonPressed = ((Button) view).getText().toString();
        // String digits = "0123456789.";

        if (DIGITS.contains(buttonPressed)) {
            // digit was pressed
            if (userIsInTheMiddleOfTypingANumber) {
                calculatorDisplay.append(buttonPressed);
            } else {
                calculatorDisplay.setText(buttonPressed);
                userIsInTheMiddleOfTypingANumber = true;
            }
        } else {
            // operation was pressed
            if (userIsInTheMiddleOfTypingANumber) {
                brain.setOperand(Double.parseDouble(calculatorDisplay.getText().toString()));
                userIsInTheMiddleOfTypingANumber = false;
            }

            brain.performOperation(buttonPressed);
            calculatorDisplay.setText(df.format(brain.getResult()));

        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save variables on screen orientation change
        outState.putDouble("OPERAND", brain.getResult());
        outState.putDouble("MEMORY", brain.getMemory());
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Restore variables on screen orientation change
        brain.setOperand(savedInstanceState.getDouble("OPERAND"));
        brain.setMemory(savedInstanceState.getDouble("MEMORY"));
        calculatorDisplay.setText(df.format(brain.getResult()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

}

CalculatorBrain.java:

package com.dva.schooltoolshome;

public class CalculatorBrain {
    // 3 + 6 = 9
    // 3 & 6 are called the operand.
    // The + is called the operator.
    // 9 is the result of the operation.
    private double operand = 0;
    private double waitingOperand = 0;
    private String waitingOperator = "";
    private double calculatorMemory = 0;

    public void setOperand(double operand) {
        this.operand = operand;
    }

    public double getResult() {
        return operand;
    }

    // used on screen orientation change
    public void setMemory(double calculatorMemory) {
        this.calculatorMemory = calculatorMemory;
    }

    // used on screen orientation change
    public double getMemory() {
        return calculatorMemory;
    }

    public String toString() {
        return Double.toString(operand);
    }

    protected double performOperation(String operator) {

        /*
        * If you are using Java 7, then you can use switch in place of if statements
        *
        *     switch (operator) {
        *     case "MC":
        *         calculatorMemory = 0;
        *         break;
        *     case "M+":
        *         calculatorMemory = calculatorMemory + operand;
        *         break;
        *     }
        */

        if (operator.equals("MC")) {
            calculatorMemory = 0;
        } else if (operator.equals("M+")) {
            calculatorMemory = calculatorMemory + operand;
        } else if (operator.equals("M-")) {
            calculatorMemory = calculatorMemory - operand;
        } else if (operator.equals("MR")) {
            operand = calculatorMemory;
        } else if (operator.equals("C")) {
            operand = 0;
            waitingOperator = "";
            waitingOperand = 0;
            calculatorMemory = 0;
        } else if (operator.equals("Sqrt")) {
            operand = Math.sqrt(operand);
        } else if (operator.equals("1/x")) {
            if (operand != 0) {
                operand = 1 / operand;
            }
        } else if (operator.equals("+/-")) {
            operand = -operand;
        } else if (operator.equals("sin")) {
            operand = Math.sin(operand);
        } else if (operator.equals("cos")) {
            operand = Math.cos(operand);
        } else {
            performWaitingOperation();
            waitingOperator = operator;
            waitingOperand = operand;
        }

        return operand;
    }

    protected void performWaitingOperation() {

        if (waitingOperator.equals("+")) {
            operand = waitingOperand + operand;
        } else if (waitingOperator.equals("*")) {
            operand = waitingOperand * operand;
        } else if (waitingOperator.equals("-")) {
            operand = waitingOperand - operand;
        } else if (waitingOperator.equals("/")) {
            if (operand != 0) {
                operand = waitingOperand / operand;
            }
        }

    }

}

activity_calculator.xml:

   <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:maxLines="1"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="0"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textSize="40sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/linearLayout1" >

            <Button
                android:id="@+id/buttonAddToMemory"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="M+" />

            <Button
                android:id="@+id/buttonSubtractFromMemory"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="M-" />

            <Button
                android:id="@+id/buttonRecallMemory"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="MR" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/linearLayout2" >

            <Button
                android:id="@+id/buttonClear"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="C" />

            <Button
                android:id="@+id/buttonToggleSign"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="+/-" />

            <Button
                android:id="@+id/buttonDivide"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="/" />

            <Button
                android:id="@+id/buttonMultiply"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="*" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/linearLayout3" >

            <Button
                android:id="@+id/button7"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="@string/button7" />

            <Button
                android:id="@+id/button8"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="@string/button8" />

            <Button
                android:id="@+id/button9"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="@string/button9" />

            <Button
                android:id="@+id/buttonSubtract"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="-" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/linearLayout4" >

            <Button
                android:id="@+id/button4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="@string/button4" />

            <Button
                android:id="@+id/button5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="@string/button5" />

            <Button
                android:id="@+id/button6"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="@string/button6" />

            <Button
                android:id="@+id/buttonAdd"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".25"
                android:text="+" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/linearLayout5"
            android:baselineAligned="false" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight=".75"
                android:orientation="vertical" >

                <LinearLayout
                    android:id="@+id/linearLayout7"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <Button
                        android:id="@+id/button1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight=".33"
                        android:text="@string/button1" />

                    <Button
                        android:id="@+id/button2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight=".33"
                        android:text="@string/button2" />

                    <Button
                        android:id="@+id/button3"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight=".34"
                        android:text="@string/button3" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/linearLayout8"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <Button
                        android:id="@+id/button0"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight=".66"
                        android:text="@string/button0" />

                    <Button
                        android:id="@+id/buttonDecimalPoint"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight=".34"
                        android:text="." />
                </LinearLayout>
            </LinearLayout>

            <Button
                android:id="@+id/buttonEquals"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight=".25"
                android:text="=" />
        </LinearLayout>

        <ImageButton
            android:id="@+id/apps"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/wbrowser"
            android:background="@drawable/appdrawer"
            android:src="@drawable/appdrawer" />

        <ImageButton
            android:id="@+id/wbrowser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:src="@drawable/browser" />

        <ImageButton
            android:id="@+id/ImageButton01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/wbrowser"
            android:background="@drawable/appdrawer"
            android:src="@drawable/appdrawer" />

        <Button
            android:id="@+id/tools"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/ImageButton01"
            android:layout_centerHorizontal="true"
            android:text="Tools" />

        <Button
            android:id="@+id/buttonClearMemory"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_above="@+id/ImageButton01"
            android:layout_alignLeft="@+id/tools"
            android:layout_marginBottom="163dp"
            android:layout_weight=".25"
            android:text="MC" />

</RelativeLayout>

我还是Android开发的新手,所以请不要判断

问候

Rapsong11

1 个答案:

答案 0 :(得分:0)

首先在每个按钮的OnClick(View v)函数中使用switch语句。 像这样。

public void onClick(View v) 
{
case R.Id.button1
// the functionality code goes here

break;

case R.id.button2

break;
}