2012-02-24 13 views
1

私はゲームのスコアボードとして使うための簡単な電卓を設計しようとしています。最終的に、私は選手の数を選択することができるようにしたい、多くのスコア追跡者が画面に表示され、タッチ計算機を使用して加算または減算(または除算または乗算)できるようにしたい部。電卓の問題 - Androidプログラミング

現在、電卓のディスプレイに入力されたテキストを取り込み、それを選んだ選手のスコアに加算/減算します。

問題は、数値キーを電卓の表示部に表示させようとしていることです。たとえば、 "1"を押してから "0"を押して、 "10"を計算機に表示させたいとします。それは私が(アンドロイドのデフォルトのキーボードを使用して)手動でテキストを入力することができるように見えるのは簡単だったはずですが、私が得ることができるのは一度に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)); 

          } 

        } 

      }); 

答えて

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> 
+0

クイックデモをありがとう!私はすべての準備ができて、この作業の計算機の部分を持って、私は電卓の "タッチパッド"の部分について心配です。具体的には、数字を押した状態でtextedit(またはtextview)フィールドを追加する方法です。たとえば、「1」ボタンを押すと、「2」ボタン(画面上)が表示され、texteditフィールドに「12」と表示されます。それはとてもシンプルに聞こえるが、私はそれを働かせることができない。私はそれを今日混乱させるつもりだし、もし私がそれを働かせることができれば投稿するだろう。その間、どんなヒントも感謝しています!! – user1231255

+0

デモでは、その方法を示しています。 outputValue + = Double.parseDouble(output.getText()。toString()); そのコードは現在editTextに含まれているものを取得し、その下の行はそれに追加されます。 – Jon

+0

はい、追加されますが、フィールドが追加されません。両方の文字列Doublesを追加すると、既存の数値の後に数値を追加するのではなく、文字数を文字数で加算して出力します。とにかくそれに取り組む時間がなかった。いくつかのアイデアがあり、フィールドを追加するチュートリアルが見つかりました。私は、現在の文字列を格納するための "見えない"テキストビューボックスを追加し、その文字列の最後に押された番号を追加して更新し、電卓のtexteditボックスに出力させることを考えています。それはラウンドアバウトのように見えるが、うまくいけばうまくいく! – user1231255

1

電卓のボタンを押すたびに追加できる文字列はありませんでしたか?その後、新しい文字列で電卓のディスプレイを更新しますか?

+0

です。私はそれを働かせようとしましたが、私は間違ったことをしているに違いありません。私はまだコーディングの100%を理解していない... – user1231255