G
G
Georgy Pliev2017-09-02 21:54:26
Java
Georgy Pliev, 2017-09-02 21:54:26

How to set reset button in Android Studio Code?

I'm taking a course in Android development, I need help with one of the tasks.
The essence of the task is this: It is necessary to create an application that will calculate the number of points in a basketball game for two teams (A and B). For each team, you need to make a scoreboard with buttons (3 points, 2 points, penalty), and by pressing the desired button, the application should add points (according to the button). And with this I successfully coped.
However, you also need to add a button to reset (reset) the results. And this is where I sat on the reefs. Help plz. I attach XML and JAVA codes to the task

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.courtcounter.MainActivity">

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="@string/Team_A"
        />

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:id="@+id/team_a_score"
        android:text="@string/_0"
        />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:text="@string/_3_Points"
        android:onClick="plusThreePoints"
        />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:text="@string/_2_points"
        android:onClick="plusTwoPoints"
        />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:text="@string/free_throw"
        android:onClick="plusOnePoint"
        />


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:text="@string/team_b"
            />

        <TextView
            android:id="@+id/team_b_score"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_marginTop="8dp"
            android:text="@string/_0"
            />

        <Button
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_marginTop="8dp"
            android:text="@string/_3_Points"
            android:onClick="plusThreePointsB"
            />

        <Button
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_marginTop="8dp"
            android:text="@string/_2_points"
            android:onClick="plusTwoPointsB"
            />

        <Button
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_marginTop="8dp"
            android:text="@string/free_throw"
            android:onClick="plusOnePointB"
            />


    </LinearLayout>

</LinearLayout>
<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:gravity="bottom">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Refresh"
            android:onClick="refreshPoints"

           />

</LinearLayout>>
</LinearLayout>


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;
    int refreshPoints = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * Этот метод дает три очка команде А при нажатии на соответствующую кнопку
     **/
    public void plusThreePoints(View View) {
        scoreTeamA = scoreTeamA + 3;
        displayForTeamA(scoreTeamA);
    }

    /**
     * Этот метод дает два очка команде А при нажатии на соответствующую кнопку
     **/
    public void plusTwoPoints(View View) {
        scoreTeamA = scoreTeamA + 2;
        displayForTeamA(scoreTeamA);
    }

    /**
     * Этот метод дает одно очко команде А при нажатии на соответствующую кнопку
     **/
    public void plusOnePoint(View View) {
        scoreTeamA = scoreTeamA + 1;
        displayForTeamA(scoreTeamA);
    }

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

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

    public void plusOnePointB(View View) {
        scoreTeamB = scoreTeamB + 1;
        displayForTeamB(scoreTeamB);


    }




    /**
     * Этот метод объявляет вывод очков для команды А (1,2,3) вместо нуля
     **/
    public void displayForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score);
        scoreView.setText(String.valueOf(score));
    }

    /**
     * Этот метод объявляет вывод очков для команды Б(1,2,3) вместо нуля
     **/
    public void displayForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score);
        scoreView.setText(String.valueOf(score));
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mr_serg77, 2017-09-03
@mr_serg77

You didn't show the code completely. But the bottom line is that add the reset button handler, and in the method do

scoreTeamA = 0;
scoreTeamB = 0;

And actually update the text in the text view.
In general throw LinerLayout, look at ConstraintLayout. (A lot of nesting is not ok).
And also use the AppCompat library (Button -> AppCompatButton, etc.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question