我该如何做“返回”? Java中的按钮? [英] How can i do a "return back" button in Java?

查看:119
本文介绍了我该如何做“返回”? Java中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以计算篮球比赛中的得分。
每个团队都有三个按钮来增加得分(+ 3,+ 2,罚球)。

I have an app that counts the score in a basketball match. There are three buttons for each team to increase the score (+3, +2, free throw).

我想创建一个按钮以返回如果用户单击错误的按钮。
无需为每个乐谱创建三个单独的按钮(+ 3,+ 2,+ 1)。但是我真的不知道如何用Java代码转换它。

I want to create a button to return back in case the user click a button for mistake. Without creating three separate buttons for each score (+3,+2,+1). But i don't really know how to transform this in Java code.

类似:Score = score-lastNumberAdded。对不起我的英语不好。
这是代码:

Something like: Score=score-lastNumberAdded. Sorry for my english. This is the code:

XML

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

}


推荐答案

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

Undo problems beg for a Stack to be used since it's a Last In First Out (LIFO) and efficient data structure. That means, in your case, every time a new score is added it will be sitting at the top. The score before that will be sitting just below it and so on and so forth. Therefore, when you start to undo you simply pop() off the top element, subtract it from the score, and re-display the score.

由于堆栈非常容易实现,并且因为您可以添加一些较小的修改以帮助您的程序,所以我认为这是您自己亲自进行的其中一次创建自己的轻量级 ScoreStack ;像这样:

Since a stack is incredibly easy to implement and because you can add couple minor modifications to aid in your program, I see this as one of those times you take it upon yourself to create your own light-weight ScoreStack; like so:

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 格式的当前分数,并且相对容易和高效地完成所有操作。当然,这会稍微改变您的设计,所以这里有个例子。

With this you're able to keep track of the ever-changing score, provide a simple means for obtaining the current score in String format, and do it all with relative ease and efficiency. Of course, this changes your design slightly so here's an example.

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);像这样:



Unrelated
Additionally, you have a number of improvements you could consider. It could be a sign the design could be better when you're providing the same functionality in methods which only differ by signature. For example, displayForTeamA() and its counterpart for TeamB could be displayScore(int id, String score); like so:

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

并称呼为:

displayScore(R.id.team_a_score,aTeamStack);

这对所有团队都是如此您拥有的特定方法。也就是说,只要花一点力气就能达到更清洁的效果。

This is true for all of the team specific methods you have. That is, with a little effort towards design you can achieve a cleaner result.

这篇关于我该如何做“返回”? Java中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆