Как задать кнопку сброса результатов в коде Android Studio?

Я прохожу курс по Android разработке, нужна помощь в одном из заданий.
Суть задания такая : Необходимо создать приложение, которое будет подсчитывать, количество очков в баскетбольном матче для двух команд (А и Б). Для каждой команды нужно сделать табло с кнопками (3 очка, 2 очка, штрафной), и по нажатию нужной кнопки приложение должно добавлять очки(в соответствии с кнопкой). И с этим я успешно справился.
Однако нужно также добавить кнопку сброса (обнуления) результатов. И вот здесь-то я и сел на рифы. Помогите плз. XML и JAVA коды прикрепляю к задаче

<?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));
    }
  • Вопрос задан
  • 251 просмотр
Пригласить эксперта
Ответы на вопрос 1
@mr_serg77
Не полностью Вы код показали кажется. Но суть в том будет, что допишите обработчик кнопки сброса, а в методе делайте
scoreTeamA = 0;
scoreTeamB = 0;


И собственно текст в текст вью обновляйте.

Вообще бросьте LinerLayout, посмотрите на ConstraintLayout. (Много вложенностей не ок).
А так же пользуйтесь библиотекой AppCompat (Button -> AppCompatButton, и т.д.)
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы