2016-10-14 5 views
1

私はバスケットボールの試合でスコアを数えるアプリを持っています。 各チームがスコアを上げるための3つのボタンがあります(+3、+2、フリースロー)。Javaで「戻る」ボタンを実行するにはどうすればよいですか?

ユーザーが誤ってボタンをクリックした場合に戻るボタンを作成したいと考えています。 スコア(+ 3、+ 2、+ 1)ごとに3つの別個のボタンを作成しないでください。しかし、私は実際にこれをJavaコードでどのように変換するのか分かりません。

次のようなものがあります。Score = score-lastNumberAdded。私の英語のために申し訳ありません。 これはコードです:

XML

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:padding="4dp" 
     android:text="Team A" 
     android:textSize="14sp" 
     android:textColor="#616161" 
     android:fontFamily="sans-serif-medium" 
     android:layout_marginTop="16dp" 
     android:layout_marginBottom="16dp" /> 

    <TextView 
     android:id="@+id/team_a_score" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:padding="4dp" 
     android:text="0" 
     android:textSize="56sp" 
     android:textColor="#000000" 
     android:fontFamily="sans-serif-light" 
     android:layout_marginBottom="24dp"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:text="+3 Points" 
     android:onClick="addThreeforTeamA" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:text="+2 Points" 
     android:onClick="addTwoforTeamA" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:text="+1 Point" 
     android:onClick="addOneforTeamA" /> 

</LinearLayout> 

<View 
    android:layout_width="1dp" 
    android:layout_height="wrap_content" 
    android:background="@android:color/darker_gray" 
    android:layout_marginTop="16dp" 
    android:layout_marginBottom="50dp"/> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:padding="4dp" 
      android:text="Team B" 
      android:textSize="14sp" 
      android:textColor="#616161" 
      android:fontFamily="sans-serif-medium" 
      android:layout_marginTop="16dp" 
      android:layout_marginBottom="16dp" /> 

     <TextView 
      android:id="@+id/team_b_score" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:padding="4dp" 
      android:text="0" 
      android:textSize="56sp" 
      android:textColor="#000000" 
      android:fontFamily="sans-serif-light" 
      android:layout_marginBottom="24dp" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:text="+3 Points" 
      android:onClick="addThreeforTeamB" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:text="+2 Points" 
      android:onClick="addTwoforTeamB" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:text="+1 Point" 
      android:onClick="addOneforTeamB" /> 

    </LinearLayout> 


</LinearLayout> 

<Button 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:text="Reset" 
android:onClick="reset" /> 

</RelativeLayout> 

JAVA

package com.example.android.courtcounter; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    int scoreTeamA=0; 
int scoreTeamB=0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    /** 
    * Displays the given score for Team A. 
    */ 
    public void displayForTeamA(int score) { 
     TextView scoreView = (TextView) findViewById(R.id.team_a_score); 
     scoreView.setText(String.valueOf(score)); 
    } 

    /** 
    * Displays the given score for Team B. 
    */ 
    public void displayForTeamB(int score) { 
     TextView scoreView = (TextView) findViewById(R.id.team_b_score); 
     scoreView.setText(String.valueOf(score)); 
    } 

    public void addThreeforTeamA(View v) { 
     scoreTeamA+=3; 
     displayForTeamA(scoreTeamA); 
    } 

    public void addTwoforTeamA(View v) { 
     scoreTeamA+=2; 
     displayForTeamA(scoreTeamA); 
    } 

    public void addOneforTeamA(View v) { 
     scoreTeamA+=1; 
     displayForTeamA(scoreTeamA); 
    } 

    public void addThreeforTeamB(View v) { 
    scoreTeamB+=3; 
    displayForTeamB(scoreTeamB); } 

    public void addTwoforTeamB(View v) { 
    scoreTeamB+=2; 
    displayForTeamB(scoreTeamB); } 

    public void addOneforTeamB(View v) { 
    scoreTeamB+=1; 
    displayForTeamB(scoreTeamB); } 

    public void reset(View v) { 
    scoreTeamA=0; 
    scoreTeamB=0; 
    displayForTeamA(scoreTeamA); 
    displayForTeamB(scoreTeamB); } 

} 
+0

あなたのように聞こえますが、そのアイデアが正しいと思います。あなたが最後に追加したスコアをロールバックできるようにしたい場合は、非常に簡単です。必要に応じて値を保存し、それをマイナスします。任意の数のアンドゥが必要な場合は、それらをすべて(おそらくスタックに)保存する必要があります。 – ChiefTwoPencils

+0

最後に追加された番号+チームコードを格納するためにArrayListを使用します。戻るボタンを押すと、最後に追加された番号を検索して取得し、チームコードをチェックしてマイナスにするだけです。 –

答えて

0

は、問題は、それがまずアウト(LIFO)で最終的かつ効率的なデータ構造ですので、使用するStackを請う元に戻します。つまり、あなたのケースでは、新しいスコアが追加されるたびに、それがトップに座ります。その前のスコアは、その真下に座っているなどのようになります。したがって、元の要素をオフに戻すだけでpop()を取り除き、それをスコアから差し引き、スコアを再表示します。

スタックは非常に実装が簡単で、プログラムに役立たせるためにいくつかの小さな変更を加えることができるので、これをあなた自身の軽量化のために取る時の1つと考えています。ScoreStack;そのように:これにより

public class ScoreStack { 
    private class ScoreNode { 
     int score; 
     ScoreNode next; 

     ScoreNode(int score, ScoreNode next) { 
      this.score = score; 
      this.next = next; 
     } 
    } 

    private int score = 0; 
    private ScoreNode root = null; 

    public void push(int score) { 
     root = new ScoreNode(score, root); 
     this.score += score; 
    } 

    public int pop() { 
     if (root == null) { 
      return 0; 
     } 
     int score = root.score; 
     root = root.next; 
     this.score -= score; 
     return score; 
    } 

    public void reset() { 
     root = null; 
    } 

    @Override 
    public String toString() { 
     return String.valueOf(score); 
    } 
} 

あなたはString形式で現在のスコアを取得するための簡単な手段を提供し、刻々と変化するスコアを追跡し、かつ比較的容易かつ効率的にそれをすべて行うことができるしています。もちろん、これはあなたのデザインを変更します少しここに例があります。



無関係
public void addOneforTeamA(View v) { 
    aTeamStack.push(1); 
    displayForTeamA(); 
} 

public void undoLastForA() { 
    aTeamStack.pop(); 
    displayForTeamA(); 
} 

public void displayForTeamA() { 
    ((TextView) findViewById(R.id.team_a_score)).setText(aTeamStack); 
} 

はまた、あなたが考えることができ、多くの改良を持っています。シグネチャだけが異なるメソッドで同じ機能を提供しているときは、デザインが優れている可能性があります。例えば、 displayForTeamA()およびそれに対応する TeamB は、となる可能性があります。 displayScore(int id, String score)です。そのよう:

public void displayScore(int id, String score) { 
    ((TextView) findViewById(id)).setText(score); 
} 

などと呼ばれる:

displayScore(R.id.team_a_score, aTeamStack);

これは、あなたが持っているチームの特定のメソッドのすべてについても同様です。つまり、設計に向けた少しの努力で、よりクリーンな結果を達成することができます。

0

は、その値ボタンが押されるたびに更新し、各チームのために追加の変数を設定します。このバージョンでは、最後の操作のみを元に戻すことができます。以前に追加したすべての値を元に戻すには、以前に追加したすべての値のリストを使用する必要があります。

チームAの例:

int lastAddA=0; 

public void addThreeforTeamA(View v) { 
    scoreTeamA+=3; 
    lastAddA = 3; 
    displayForTeamA(scoreTeamA); 
} 

public void undoLastTeamA(View v) { 
    scoreTeamA-=lastAddA; 
    lastAddA = 0;//Reset to default 
    displayForTeamA(scoreTeamA); 
} 
関連する問題