私はバスケットボールの試合でスコアを数えるアプリを持っています。 各チームがスコアを上げるための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); }
}
あなたのように聞こえますが、そのアイデアが正しいと思います。あなたが最後に追加したスコアをロールバックできるようにしたい場合は、非常に簡単です。必要に応じて値を保存し、それをマイナスします。任意の数のアンドゥが必要な場合は、それらをすべて(おそらくスタックに)保存する必要があります。 – ChiefTwoPencils
最後に追加された番号+チームコードを格納するためにArrayListを使用します。戻るボタンを押すと、最後に追加された番号を検索して取得し、チームコードをチェックしてマイナスにするだけです。 –