【问题标题】:Calculator Problems - Android Programming计算器问题 - Android 编程
【发布时间】:2012-02-24 17:13:36
【问题描述】:

我正在尝试设计一个简单的计算器,用作一两场比赛的记分牌。最终,我希望能够选择玩家的数量,在屏幕上显示那么多的得分跟踪器,能够使用触摸计算器进行加减(或除法或乘法) 我让它工作得最多部分。

目前,它采用在计算器显示中输入的文本,对我选择的球员分数进行加/减/任何运算。

问题是试图让数字键显示在计算器显示部分。例如,我希望能够点击“1”然后点击“0”并让“10”出现在计算器中。这应该很容易,因为我可以手动输入文本(使用 android 默认键盘),但我能得到的最接近的是一次只显示 1 个数字......

长话短说,我正在尝试让触摸板型计算器的数字按钮工作和显示。下面是我的主要项目代码。如果您需要我的布局代码,我也可以发布(用于引用 ID)。

我知道这可能是一个简单的解决方案,但我发现的每个教程要么过于复杂,要么不起作用......非常感谢任何帮助!

package com.MCalculator8.test;

import com.MCalculator8.test.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MCalculator8Activity extends Activity {
    private EditText player1name;
    private EditText player2name;
    private EditText player3name;
    private EditText player4name;
    private EditText player5name;
    private EditText player6name;
    private EditText player7name;

    private EditText player1score;
    private EditText player2score;
    private EditText player3score;
    private EditText player4score;
    private EditText player5score;
    private EditText player6score;
    private EditText player7score;

    private EditText input;

    private TextView operator;
    private MCalculator8Activity mContext;

    // Called when the activity is first created. 
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mContext = this;

            setContentView(R.layout.main);

            player1name = (EditText) findViewById(R.id.player1name);
            player2name = (EditText) findViewById(R.id.player2name);
            player3name = (EditText) findViewById(R.id.player3name);
            player4name = (EditText) findViewById(R.id.player4name);
            player5name = (EditText) findViewById(R.id.player5name);
            player6name = (EditText) findViewById(R.id.player6name);
            player7name = (EditText) findViewById(R.id.player7name);

            input = (EditText) findViewById(R.id.input);

            player1score = (EditText) findViewById(R.id.player1score);
            player2score = (EditText) findViewById(R.id.player2score);
            player3score = (EditText) findViewById(R.id.player3score);
            player4score = (EditText) findViewById(R.id.player4score);
            player5score = (EditText) findViewById(R.id.player5score);
            player6score = (EditText) findViewById(R.id.player6score);
            player7score = (EditText) findViewById(R.id.player7score);

            operator = (TextView) findViewById(R.id.operator);

            // We create an OnClick Event in each button.

            Button plusButton = (Button) findViewById(R.id.add);
            Button minusButton = (Button) findViewById(R.id.subtract);
            Button multiplyButton = (Button) findViewById(R.id.multiply);
            Button player1equals = (Button) findViewById(R.id.player1equals);
            Button player2equals = (Button) findViewById(R.id.player2equals);
            Button player3equals = (Button) findViewById(R.id.player3equals);
            Button player4equals = (Button) findViewById(R.id.player4equals);
            Button player5equals = (Button) findViewById(R.id.player5equals);
            Button player6equals = (Button) findViewById(R.id.player6equals);
            Button player7equals = (Button) findViewById(R.id.player7equals);

            plusButton.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {

                            operator.setText("+");

                    }

            });

            minusButton.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {

                            operator.setText("-");

                    }

            });

            multiplyButton.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {

                            operator.setText("x");

                    }

            });


            player1equals.setOnClickListener(new OnClickListener() {

                    private AlertDialog show;

                    public void onClick(View arg0) {

                            if ((input.getText().length() == 0)
                                            || (input.getText().toString() == " ")) {
                                         //   || (input2.getText().length() == 0)
                                         //   || (input2.getText().toString() == " ")) {

                                    show = new AlertDialog.Builder(mContext).setTitle("Error")
                                                    .setMessage("Some inputs are empty")
                                                    .setPositiveButton("OK", null).show();

                            } else if (operator.getText().equals("")) {

                                    show = new AlertDialog.Builder(mContext).setTitle("Error")
                                                    .setMessage("Operator is null").setPositiveButton(
                                                                    "OK", null).show();

                            } else if (operator.getText().equals("+")) {

                                    double result = new Double(input.getText().toString())
                                                    + new Double(player1score.getText().toString());

                                    player1score.setText(Double.toString(result));

                            } else if (operator.getText().equals("-")) {

                                    double result = new Double(player1score.getText().toString())
                                                    - new Double(input.getText().toString());

                                    player1score.setText(Double.toString(result));

                            } else if (operator.getText().equals("x")) {

                                    double result = new Double(input.getText().toString())
                                                    * new Double(player1score.getText().toString());


                                    player1score.setText(Double.toString(result));

                            }

                    }

            });

【问题讨论】:

    标签: android text calculator touchpad


    【解决方案1】:

    这个例子怎么样?

    这里是名为“CalculatorExample.java”的主类

    package com.calculatorExample;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class CalculatorExample extends Activity implements android.view.View.OnClickListener{
        Button add, subtract, multiply, divide;
        TextView firstInput, secondInput, output;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // Reference TextViews and Buttons
            firstInput = (TextView) findViewById(R.id.firstIput);
            secondInput = (TextView) findViewById(R.id.secondInput);
            output = (TextView) findViewById(R.id.output);
            add = (Button) findViewById(R.id.add);
            subtract = (Button) findViewById(R.id.subtract);
            multiply = (Button) findViewById(R.id.multiply);
            divide = (Button) findViewById(R.id.divide);
    
            // Set listeners for when buttons are pressed
            add.setOnClickListener(this);
            subtract.setOnClickListener(this);
            multiply.setOnClickListener(this);
            divide.setOnClickListener(this);
    
        }
    
        /**
         * Switch statement to decide which button was pressed
         */
        public void onClick(View arg0) {
            // Get values from top two TextViews
            double firstInputValue = Double.parseDouble(firstInput.getText().toString());
            double secondInputValue = Double.parseDouble(secondInput.getText().toString());
            // Initialise output
            double outputValue = 0;
    
            // Perform relevant operations
            switch(arg0.getId()){
            case R.id.add:
                outputValue = firstInputValue + secondInputValue;
                break;
            case R.id.subtract:
                outputValue = firstInputValue - secondInputValue;
                break;
            case R.id.multiply:
                outputValue = firstInputValue * secondInputValue;
                break;
            case R.id.divide:
                outputValue = firstInputValue / secondInputValue;
                break;
            }
            // Add result to Running total stored in output TextView
            outputValue += Double.parseDouble(output.getText().toString());
            output.setText("" + outputValue);
    
        }
    }
    

    这里是 XML 文件(“main.xml”)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout 
            android:weightSum="100"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
        >
            <EditText 
                android:layout_weight="50"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/firstIput">
            </EditText>
            <EditText 
                android:layout_weight="50"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:id="@+id/secondInput">
            </EditText>
        </LinearLayout>
        <LinearLayout 
            android:weightSum="100"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
        >
            <Button 
                android:layout_weight="50"
                android:text="+" 
                android:id="@+id/add" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </Button>
            <Button 
                android:layout_weight="50"
                android:text="-" 
                android:id="@+id/subtract" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </Button>
        </LinearLayout>
        <LinearLayout 
            android:weightSum="100"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
        >
            <Button 
                android:layout_weight="50"
                android:text="X" 
                android:id="@+id/multiply" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </Button>
            <Button 
                android:layout_weight="50"
                android:text="/" 
                android:id="@+id/divide"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </Button>
        </LinearLayout>
        <EditText 
            android:text="0.0"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:id="@+id/output">
        </EditText>
    </LinearLayout>
    

    【讨论】:

    • 感谢您的快速演示!我都准备好了这个工作的计算器部分,我担心计算器的“触摸板”部分。更具体地说,如何在按下数字的情况下附加文本编辑(或文本视图)字段。例如,按“1”按钮,然后按“2”按钮(在屏幕上)会导致文本编辑字段显示“12”。这听起来很简单,但我就是无法让它工作。我今天要搞砸一些,如果我能让它工作,我会发布。同时,感谢任何提示!
    • 那个演示展示了如何做到这一点。 outputValue += Double.parseDouble(output.getText().toString());该代码获取当前在 editText 中的内容,并将其下方的行添加到该内容中。
    • 是的,但它会添加它,它不会附加该字段。如果我添加两个字符串双精度数,它实际上会添加数字并输出总和,而不是在现有数字之后添加数字。反正我还没有时间去研究它。我有一些想法,并找到了有关附加字段的教程。我正在考虑添加一个“不可见”的文本视图框来存储当前字符串,并将按下的数字添加到该字符串的末尾并更新它,然后将其输出到计算器的文本编辑框。这似乎是一种迂回的方式,但希望它会奏效!
    • 那也应该在代码中,只需在 EditText 的内容后面加上数字 ("" + num)。
    • 好的,我明白了!!非常感谢你,乔恩!你帮了大忙!解决方案非常简单,我真的想踢自己。我所要做的就是: EditText et = (EditText) findViewById(R.id.textedit); et.append("c");我无法相信它是多么简单 x(谢谢大家!问题已得到解答!
    【解决方案2】:

    难道您不能在每次按下计算器上的按钮时附加一个字符串吗?然后用新字符串更新计算器显示?

    【讨论】:

    • 这让我很困惑。我试图让它发挥作用,但我一定做错了什么。我还没有 100% 理解编码...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 2018-07-31
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多