【问题标题】:How can i do a "return back" button in Java?如何在 Java 中执行“返回”按钮?
【发布时间】: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


【解决方案1】:

撤消问题需要使用Stack,因为它是一种后进先出 (LIFO) 和高效的数据结构。这意味着,在您的情况下,每次添加新分数时,它都会位于顶部。之前的分数将位于它的正下方,依此类推。因此,当您开始撤消时,您只需将顶部元素pop() 从分数中减去,然后重新显示分数。

由于堆栈非常容易实现,并且您可以添加一些小的修改来帮助您的程序,我认为这是您自己创建自己的轻量级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);

这适用于您拥有的所有团队特定方法。也就是说,只要在设计上稍加努力,就可以获得更清晰的结果。

【讨论】:

    【解决方案2】:

    为每个团队设置一个附加变量,每次按下按钮时更新其值。此版本只能撤消最后一个操作。如果您希望能够撤消所有先前添加的值,则必须使用所有先前添加的值的列表。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 2014-11-22
      • 2012-11-20
      相关资源
      最近更新 更多