【发布时间】:2016-10-14 21:55:34
【问题描述】:
我有一个计算篮球比赛得分的应用。 每支球队有三个按钮可以增加得分(+3、+2、罚球)。
我想创建一个返回按钮,以防用户误点击按钮。 无需为每个乐谱创建三个单独的按钮(+3、+2、+1)。但我真的不知道如何在 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); }
}
【问题讨论】:
-
听起来你的想法是正确的。如果您只希望用户能够回滚最后添加的分数,这很容易。保存该值并在需要时将其减去。如果您想要任意数量的撤消,则需要将它们全部保存(可能在堆栈中)。
-
使用 ArrayList 存储最后添加的号码 + 团队代码。按下后退按钮时,只需检查并检索最后添加的号码,然后通过检查团队代码来减去..
标签: java android algorithm android-layout android-button