Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

text - Calculator Problems - Android Programming

I'm trying to design a simple calculator for use as a scoreboard for a game or two. Ultimately, I want to be able to select the number of players, have that many score-trackers appear on screen, be able to use a the touch calculator to add or subtract (or divide or multiply) I got it to work for the most part.

Currently, it takes the text inputted in the calculator display, adds/subtracts/whatever that to the player score that I choose.

The problem is trying to get the numerical keys to show up in the calculator display part. For instance, I want to be able to hit "1" then "0" and have "10" appear in the calculator. It should have been easy, seeing as I am able to input text MANUALLY (using the android default keyboard), but the closest I can get is for only 1 number to show up at a time...

Long story short, I am trying to get a touch-pad type calculator's numerical buttons to work and display. Below is my main project code. If you need my layout code, I can post that as well (for references to IDs).

I know it's probably a simple solution, but every tutorial I find is either overly complicated or does not work... ANY help is greatly appreciated!

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));

                            }

                    }

            });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Couldn't you have a String that you could append to every time a button on the calculator is pressed? Then update the calculator display with the new String?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...